Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Open
Description
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
Repro Code
constobj:{}={}// works as expectedconstvalues=Object.values(obj)constassertedValues=valuesasstring[];// false positiveconstdirectlyAssertedValues=Object.values(obj)asstring[];
ESLint Config
{"rules":{"@typescript-eslint/no-unnecessary-type-assertion":"error"}}
tsconfig
{"compilerOptions": {"strictNullChecks":true }}
Expected Result
There should be no error in the provided playground
Actual Result
There’s an error in line 8:
This assertion is unnecessary since it does not change the type of the expression. 8:32 - 8:62
Additional Info
It seems that when we haveObject.values(obj)
that returns anArray<unknown>
, and we’d want to assert that to a more specific array, the rule thinks the assertion is unnecessary when it’s actually necessary.
Note that this doesn’t happen when we changeobj
to beRecord<string, unknown>
, so maybe this is specific to the type{}
. I’m aware this type should generally be avoided, but I’m not sure how this makes a difference here.
The error can also be reproduced by inlining the object:Object.values({})
.