You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regression: IndexedAccess T[keyof T] is not assignable to anything #21368
Search Terms: indexed access, type parameter keyof
Code
interfaceI{foo: string;}declarefunctiontake<T>(p: T): void;functionfn<TextendsI,KextendskeyofT>(o: T,k: K){take<string>(o[k]);// Argument of type 'T[K]' is not assignable to parameter of type 'string'.take<{}|null|undefined>(o[k]);// Type 'T[K]' is not assignable to type '{} | null | undefined'. Type 'T[K]' is not assignable to type '{}'.take<any>(o[k]);// this one works}
Expected behavior:
Compiles without error. 2.7.0-rc allows all of the above, although the type of the indexed access is not correctly inferred, see #12991 (comment)
Actual behavior:
Error on the first two calls, see inline comments.
changed the title [-]IndexedAccess T[keyof T] is assignable to nothing[/-][+]Regression: IndexedAccess T[keyof T] is not assignable to anything[/+]on Jan 25, 2018
It looks like the first call to take shouldn't work, since there's no guarantee T[K] is a string. T could be an object that extends I and adds non-string properties.
But the second one should work. Previously, anything other than null and undefined could be assigned to the type '{}', but that doesn't seem to be the case anymore. Is this a bug, or by design?
that is a special case really. the general case is that you can not make assumptions about o[k]. we could add a special handling to allow it to be assignable to that type.
Got it - thanks for clearing that up! Not sure if special-casing for {} | null | undefined makes sense or not (especially since there are reasonable alternatives).
We talked about the unknown type one more time today. Ryan Cavanaugh (@RyanCavanaugh) is working on a proposal here. it is going to be a new primitive type.
Reacted by Jack Williams
Reacted by Tom, Bnaya Peretz, ZpdDG4gta, Simon Buchan and Tom Crockett
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
[-]IndexedAccess T[keyof T] is assignable to nothing[/-][+]Regression: IndexedAccess T[keyof T] is not assignable to anything[/+]tomblind commentedon Feb 1, 2018
It looks like the first call to take shouldn't work, since there's no guarantee T[K] is a string. T could be an object that extends I and adds non-string properties.
But the second one should work. Previously, anything other than null and undefined could be assigned to the type '{}', but that doesn't seem to be the case anymore. Is this a bug, or by design?
mhegazy commentedon Feb 1, 2018
Consider calling your function with:
tomblind commentedon Feb 1, 2018
So, just to confirm, the error in the following code is intended?
Is there something T[keyof T] could be that isn't assignable to {} | null | undefined ?
mhegazy commentedon Feb 1, 2018
that is a special case really. the general case is that you can not make assumptions about
o[k]. we could add a special handling to allow it to be assignable to that type.tomblind commentedon Feb 2, 2018
Before 2.7 that assignment worked, although maybe it shouldn't have. I have found a workaround using mapped types, however:
mhegazy commentedon Feb 2, 2018
Please see my response in #21369 (comment)
tomblind commentedon Feb 2, 2018
Got it - thanks for clearing that up! Not sure if special-casing for {} | null | undefined makes sense or not (especially since there are reasonable alternatives).
mhegazy commentedon Feb 6, 2018
We talked about the unknown type one more time today. Ryan Cavanaugh (@RyanCavanaugh) is working on a proposal here. it is going to be a new primitive type.
typescript-bot commentedon Feb 20, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.