8000 Bug: @typescript-eslint/no-unnecessary-type-assertion False positive on non-null assertion after an implicitly-any variable gets initialised inside a conditional or loop · Issue #11054 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
8000

Bug: @typescript-eslint/no-unnecessary-type-assertion False positive on non-null assertion after an implicitly-any variable gets initialised inside a conditional or loop #11054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
4 tasks done
ExplodingCabbage opened this issue Apr 9, 2025 · 1 comment · May be fixed by #11082
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ExplodingCabbage
Copy link

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.8.2&fileType=.ts&code=GYVwdgxgLglg9mABAUwB4EMC2AHANsgCgEpEBvAKEUXykVQG5LEZhECBZdKACwDoAndGAAmcTMUQA%2BRAFoAjCQpUqqRAF5EAJkZUAvk37IoIfklQBCRAGpEcxvvJoseQkUZA&eslintrc=N4KABGBECmAeAu0B2ATAzpAXGA2uCUADgDYCuA5gJZKYAC8AnodGgMYBOlh8AtC8dXgB6dtFYB7ALaTkKaCh6NmPVgAsxAa3mR8AXQA0%2BSO1LEWWMMAC%2BIK0A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A&tokens=false

Repro Code

function example() {
  let x;
  if (Math.random() > -1) {
    x = 2;
  }
  return x! + 1;
}

example();

ESLint Config

import tseslint from 'typescript-eslint';

export default tseslint.config(
  tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: {
        projectService: true,
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
  {
    files: ['eslint.config.mjs'],
    extends: [tseslint.configs.disableTypeChecked],
  }
);

tsconfig

{
    "include": ["**/*.ts"],
    "compilerOptions": {
        "strict": true,
        
    }
}

Expected Result

No errors, since the non-null assertion (!) is necessary here; without it, tsc will refuse to compile the code, complaining that `'x' is possibly 'undefined'!

Actual Result

6:10  error  This assertion is unnecessary since it does not change the type of the expression  @typescript-eslint/no-unnecessary-type-assertion

Additional Info

Things that seem necessary to produce the error above:

  • x is not explicitly typed when declared (so is implicitly any)
  • the initial assignment of x takes place inside a conditional or a loop, and TypeScript therefore can't tell, without the non-null assertion, that the initial assignment is guaranteed to take place
  • strict: true is enabled

The example code above is of course contrived, but a more realistic example where this would in fact be totally reasonable code would be one where, instead of an if (...) {...} that's guaranteed to run, the assignment takes place within a loop that's guaranteed to run (and x gets used to store some sort of information about the final iteration of the loop that gets used after the loop finishes).

@ExplodingCabbage ExplodingCabbage added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Apr 9, 2025
@ronami
Copy link
Member
ronami commented Apr 9, 2025

Interesting, seems like a missed edge case in isPossiblyUsedBeforeAssigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants
0