Skip to content

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

Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions