Skip to content
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

"implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer." in for-of with strictNullChecks and noImplicitAny #23284

Closed
NaridaL opened this issue Apr 9, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@NaridaL
Copy link
Contributor

NaridaL commented Apr 9, 2018

TypeScript Version: 2.9.0-dev.20180409

Search Terms: referenced directly or indirectly in its own initializer

Code
Note: strictNullChecks and noImplicitAny need to be on. As far as I can tell, all the lines are necessary to reproduce the error:

declare const elements: Element[] | undefined
declare const elMap: Map<Element, Element[][]>
if (elements) {
    for (const elements2 /* ERROR*/ of elMap.get(elements[0])!) {
        if (elements !== elements2) {}
    }
}

Expected behavior:
No error. elements2 should be typed as Element[]

Actual behavior:
'elements2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer. This is especially jarring because explicitly typing the variable of a for-loop is not allowed!

Playground Link: playground
Related Issues:
#12101 same error in same location which has been fixed. The more specific circumstances here still cause it though.

@NaridaL NaridaL changed the title "implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer." in for-of "implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer." in for-of with strictNullChecks and noImplicitAny Apr 9, 2018
@ghost
Copy link

ghost commented Apr 9, 2018

Duplcate of #19955 (Basically same as the containsItself example)

@ghost ghost added the Duplicate An existing issue was already created label Apr 9, 2018
@NaridaL NaridaL closed this as completed Apr 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

1 participant