-
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
Two level deep indexed access: read works, write fails #47352
Comments
@jcalz seems you are right. I previously tested with a different return type for each of the Values with interface Lookup {
[A.First]: number
[A.Second]: string
}
type Values = { [a in A]: { [b: string]: Lookup[a] }} This is where 4.6 did come in to play. Simplifying the problem further I must have forgotten to check if it persisted in older versions. Have updated the issue. |
As @jcalz pointed out it does seem related to #30769. In particular the following:
function f3<T extends { [key: string]: any }>(obj: T) {
let foo = obj['foo'];
let bar = obj['bar'];
obj['foo'] = 123; // Error
obj['bar'] = 'x'; // Error
} If in this case |
On read you're allowed to fall back to the constraint type, but as noted above this isn't allowed during a write |
@RyanCavanaugh I can't say I understand why, that may be way above my pay grade :) But it seems to me const valueB = values[a][b];
values[a][b] = valueB; should never fail given no changes in |
To correctly analyze that, the type of |
Bug Report
Reading from a two level deep Indexed Access type works, but writing gives "Type cannot be used to index".
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Indexing a type while reading works, while writing fails with the same indexer.
🙂 Expected behavior
Indexed reads and writes both work when using the same indexer.
The text was updated successfully, but these errors were encountered: