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

Cannot index record with own key type #45159

Closed
1 of 4 tasks
Nixinova opened this issue Jul 22, 2021 · 3 comments
Closed
1 of 4 tasks

Cannot index record with own key type #45159

Nixinova opened this issue Jul 22, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Nixinova
Copy link

Bug Report

🔎 Search Terms

  • error = Element implicitly has an 'any' type because expression of type X can't be used to index type 'Record<X, Y>'
  • template literal tag type invalid in key

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions 4.0.x and 4.1.x
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries
  • I was unable to test this on prior versions

⏯ Playground Link

Playground link with relevant code

💻 Code

type Ver = `${string}.${string}`

const data: Record<Ver, string> = {
    '1.0': 'foo',
    '1.1': 'bar',
}

let ver: Ver = '1.0'
data[ver] // Error 7053

🙁 Actual behavior

Versions[ver] errors with Element implicitly has an 'any' type because expression of type '`${string}.${string}`' can't be used to index type 'Record<`${string}.${string}`, string>'. (??)

🙂 Expected behavior

TypeScript recognises that `${string}.${string}` === the key of Record<`${string}.${string}`,string>

@Nixinova Nixinova changed the title Cannot index record with own type Cannot index record with own key type Jul 22, 2021
@Nixinova
Copy link
Author

Nixinova commented Jul 23, 2021

Seems to be a duplicate of #40193 which in turn is marked as a duplicate of #37448 though I don't really understand the latter as the issue is philosophically worded.

Why doesn't my code work and how can I fix it? I'll close when that's answered.

@ahejlsberg
Copy link
Member

Duplicate of #42192. Fixed in 4.4-beta and later.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Jul 23, 2021
@Nixinova
Copy link
Author

Oh, okay, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants