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 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
Repro Code
const obj: {} = {}
// works as expected
const values = Object.values(obj)
const assertedValues = values as string[];
// false positive
const directlyAssertedValues = Object.values(obj) as string[];
ESLint Config
{
"rules": {
"@typescript-eslint/no-unnecessary-type-assertion": "error"
}
}
tsconfig
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 have Object.values(obj)
that returns an Array<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 change obj
to be Record<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({})
.
Metadata
Metadata
Assignees
Labels
Go ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issueSomething isn't workingSomething isn't workingIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin