Skip to content

Bug: [no-unnecessary-type-assertion] false positive on evolving type #10334

Closed
@OpportunityLiu

Description

@OpportunityLiu

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.6.3&fileType=.tsx&code=LAKAJgpgxgNghgJwgAgGYFcB2UAuBLAe0zQIIAoBKALmU3QFsAjCBZAH2S0lT0wjFCRYiFBmz4iyRoko06TFoOjwkaLLkLFpALzKY5DZgmrIAbgTxgA3KFBiNk%2BnF4BGSsgDeoZMhgQcyAAeNiA%2BgcgAvFIyFCFhkSTksd7RuoEAhMkgAL62IPYSxE68AEzuXqG%2B-kFxQQnSCJS14VGopE0pOmSBWbkgQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg10NM8AOXapUAYQAW6aAGsylDul4BfECqA&tokens=false

Repro Code

declare function foo(): number | undefined
declare function bar(): number
declare function baz(n: number): void;

function main1() {
  let x;
  x = bar();
  x = foo();
  baz(x!);
}

function main2() {
  let x;
  x = bar();
  x = foo();
  baz(x);
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  },
};

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

The linter should not treat baz(x!) as an unnecessary type assertion

Actual Result

@typescript-eslint/no-unnecessary-type-assertion rule warns about the assertion baz(x!) as unnecessary, suggesting it could be removed. Removing the assertion causes TypeScript to raise an error.

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingexternalThis issue is with another package, not typescript-eslint itselflocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy