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
Bug: [no-unnecessary-type-assertion] Does not flag on unnecessary nonnull assertion on declare
d value
#8794
Comments
This likely occurs because the rule has logic to determine if the non-null assertion is a "definite assignment" assertion. Variables can be declared without a nullish type and not be assigned a value. When we inspect the type at the usage it is a non-nullish type. So a non-null assertion looks unnecessary, but it is actually necassery (removing it would be a type error). |
Oh, fascinating...... and a quick look in the code shows also that you've thought about this before, too :) microsoft/TypeScript#31124 Just to be clear, though, which of the following do you mean by your response?
|
Cases like this is what I'm referring to let x: number;
x!.toFixed(); If you remove the non-null assertion then you get a TS error. This sort of code looks bad but there's usually a callback function between the assignment which TS's can't track. |
Gotcha, yeah, so that seems clear to me, since that's literally a (tricky) case of a necessary nonnull assertion. Do you think, though, that that pattern would apply to variables declared with the |
Based on some quick testing - no it does not apply to |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.4.3&fileType=.ts&code=CYUwxgNghgTiAEYD2A7AzgF3gTwFzxQFcBbAIxBgG4AoZdJCEAOgiQHMAKbAQgEpKgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
I expect the second line to report an error.
Actual Result
No error.
Additional Info
The snippet
flags as expected. For some reason just
declare
seems to mess it up.The text was updated successfully, but these errors were encountered: