-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Writes to indexed access of an index signature are not sufficiently constrained #60703
Comments
Phrased as a feature request: When checking assignability of |
It's still the same design limitation. |
TS is often unsound in the face of writes. Reminds me of #9825 (comment). A cure might well be worse than the disease. |
It wouldn't surprise me, however it's not immediately obvious to me that fixing this particular soundness issue would cause more harm than any other type system change (this behavior doesn't seem like something people would intentionally rely on). But what do I know? |
I'm not opposed to looking at a PR for this and assessing the fallout. |
🔎 Search Terms
"return type" "generic" "type parameter" "indexed access" "index signature" "assignable" "constraint"
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.7.2#code/C4TwDgpgBAogHgQwLZgDbQLxQN4CgpQIBcUA5AqflANoDWEIJAzsAE4CWAdgOYC6zbLt1wBfXLgAmEAMaoEraNID2nFlAiIU6EvGRoIuZauBR6IAIwCOPKFnKUNe9HQbnetspyUmKUAPR+UF7qrKxKrOJSsvKKKmpmAEwk9lAAPmQARg6a+i4gCe52Xj6k-oEQoeEANIRM6nCQ0sAQEkA
💻 Code
🙁 Actual behavior
The index signature allows any
string
value to be assigned, ignoring the narrower type ofa
.🙂 Expected behavior
Both property assignments in the above code should raise errors, for the same reason.
Additional information about the issue
This is a more generalized version of #60700. @MartinJohns helped me realize that type parameters need not be involved.
Here's another pair of examples (1, 2) which I expected to be reasoned through similarly:
In both cases
Example
carries with it the information that the propertya
must have a value assignable to'a'
(as can be seen withexample.a = 'not a'
). But this is not enforced when assigning to dynamic property using a key whose type matches the index signature.Here's another example which does produce the error I expect:
The text was updated successfully, but these errors were encountered: