Skip to content

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

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
plantain-00 opened this issue Nov 28, 2022 · 1 comment

Comments

@plantain-00
Copy link
Contributor

plantain-00 commented Nov 28, 2022

Bug Report

πŸ”Ž Search Terms

implicitly has type 'any' 7022

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

let children: string | A[] = []
while (typeof children !== 'string') {
    const index = children.length - 1
    children = children[index].children
}

interface A {
  children: A[] | string
}

πŸ™ Actual behavior

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

πŸ™‚ Expected behavior

No error here

@MartinJohns
Copy link
Contributor

Duplicate of #43047. The type of index depends on the type of children, the type of children depends on the type of index (due to the reassignment).

Just add a type annotation.

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