Skip to content

Erroneous "implicitly has type any" for variable later assigned to in loop #43409

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

Closed
JoshuaKGoldberg opened this issue Mar 29, 2021 · 2 comments

Comments

@JoshuaKGoldberg
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

variable declaration equals implicit any loop

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

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!
    }
}

Originally reported in: typescript-eslint/typescript-eslint#2248

πŸ™ Actual behavior

'next' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

If you removestate = next! or the for (;;), the code exhibits the expected behavior.

πŸ™‚ Expected behavior

next is inferred to be type State | undefined.

@MartinJohns
Copy link
Contributor

#36687

@JoshuaKGoldberg
Copy link
Contributor Author

Ha, nice, thank you πŸ˜„

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants