Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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

Open
Labels
accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@ExplodingCabbage

Description

@ExplodingCabbage

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 havesearched for related issues and found none that matched my issue.
  • I haveread 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

functionexample(){letx;if(Math.random()>-1){x=2;}returnx!+1;}example();

ESLint Config

importtseslintfrom'typescript-eslint';exportdefaulttseslint.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 implicitlyany)
  • the initial assignment ofx 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 anif (...) {...} that's guaranteed to run, the assignment takes place within aloop that's guaranteed to run (andx 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 issuebugSomething isn't workingpackage: 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


      [8]ページ先頭

      ©2009-2025 Movatter.jp