-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[no-unnecessary-type-assertion] false positive in strict mode #2248
Comments
This looks like a bug in typescript. This is obviously incorrect, as internally it's able to resolve the type of Please file an issue with typescript - microsoft/TypeScript#36687 |
I reduced this down a bit to still get interface State {}
interface Foo {}
declare function initialState(): State | null
declare function nextState(state: State): [Foo, State] | undefined
export function bar(): void {
let state = initialState()
if (state == null) return
for (;;) {
const [_, next]: [Foo, State] | [] = nextState(state) ?? []
state = next!
}
} If you remove interface State {}
declare function initialState(): State | null
declare function nextState(state: State | null): [State] | undefined
export function bar(): void {
let state = initialState()
if (state == null) return
for (;;) {
const [next]: [State] | [] = nextState(state) ?? []
// ~~~~ 'next' implicitly has type 'any' because it does not have a
// type annotation and is referenced directly or indirectly in its own initializer.
state = next!
}
} That erroneous error goes away and @bradzacher should this issue be closed, since the bug seems pretty clearly in TypeScript land? |
I think we should leave this open to help stop people from raising a duplicate. |
I can no longer reproduce this on the latest versions (playground). What about others? |
Repro
Expected Result
No error.
Actual Result
With code posted above ESLint reports
however, if any of those assertions is removed TypeScript instead reports
Additional Info
TypeScript error is only present when compiling in strict mode. The fact that
initialState
returnsState | null
also seems important; if it is changed to simply returnState
and the null check in line 12 is removed then, while TypeScript still requires type assertions, ESLint stops reporting any problems.Versions
@typescript-eslint/eslint-plugin
3.4.0
@typescript-eslint/parser
3.4.0
TypeScript
3.9.5
ESLint
7.3.1
node
12.16.3
npm
6.14.4
yarn
1.12.4
The text was updated successfully, but these errors were encountered: