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

TypeScript 5.5 "Control Flow Narrowing for Constant Indexed Accesses" is not working #58883

Closed
Alexsey opened this issue Jun 16, 2024 · 2 comments

Comments

@Alexsey
Copy link

Alexsey commented Jun 16, 2024

🔎 Search Terms

5.5 Control Flow Narrowing for Constant Indexed Accesses

🕗 Version & Regression Information

  • This is a crash - No
  • This changed in commit
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.1-rc#code/C4TwDgpgBA0lC8UDkAPJUA+yRIFC9EigHkEoBvXKaqFALigDsBXAWwCMIAnXAX3wAmEAMYAbAIZdowgPaMAzsCgBrBjFxCxk6XMVQZDYvgCWAMygAKZVGON9ASgpUashTNEQAdKJkBzCzIA2soAuvZ8QA

💻 Code

type K = 'x' | 'y'

type O = {
    x: number
}

declare const k: K
declare const o: O

if (k in o) {
  // Element implicitly has an 'any' type because expression of type 'K' can't be used to index type 'O'.
  // Property 'y' does not exist on type 'O'.(7053)
    console.log(o[k])
}

🙁 Actual behavior

The error is present in the v5.5.1-rc. The error text:

Element implicitly has an 'any' type because expression of type 'K' can't be used to index type 'O'. Property 'y' does not exist on type 'O'.(7053)

🙂 Expected behavior

The error should've been resolved in the TypeScript 5.5

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

What makes you think this should work? Neither the commit nor the blog mentions the in operator.

@Alexsey
Copy link
Author

Alexsey commented Jun 17, 2024

@MartinJohns you are correct, I mistakingly assumed a broader feature scope. Anyway, considering this logic TypeScript cannot guess a better type for O[k] then unknown

@Alexsey Alexsey closed this as completed Jun 17, 2024
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