Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Open
@ExplodingCabbage

Description

@ExplodingCabbage
Issue body actions

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.