String index into a generic type having a string index signature isn't assignable to indexed access type #51127
Labels
Needs Proposal
This issue needs a plan that clarifies the finer details of how it could be implemented.
Suggestion
An idea for TypeScript
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
🔎 Search Terms
string index signature; indexed access; generic; 2322; not assignable
🕗 Version & Regression Information
This seems to be there all the way back to TS3.5, maybe introduced by #30769, although I'm not sure I understand why.
-This changed between versions 3.3.3 and 3.5.1
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Reading a property with a
string
key from aFoo[K]
object does not produce a value of typeFoo[K][string]
, even in a non-widening position.🙂 Expected behavior
Indexing into a
Foo[K]
value with astring
key should produce a property of typeFoo[K][string]
if such a type is in a non-widening position.This might be similar to #31661 and #33181 but I can't tell.
Note that numeric index signatures work as expected:
And known literal keys work as expected:
But string index signatures (and symbol also) fail for some reason.
This can be worked around easily enough:
But I'm wondering what's going on.
Playground link with above examples
(note to self: go back to this SO answer if I acquire more cluefulness here)
The text was updated successfully, but these errors were encountered: