-
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
Regression: IndexedAccess T[keyof T] is not assignable to anything #21368
Comments
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? |
Consider calling your function with: interface A extends I {
bar: number;
}
declare var a: A;
fn(a, "bar"); |
So, just to confirm, the error in the following code is intended? type unknown = {} | null | undefined;
function fn<T>(o: T, k: keyof T) {
var v: unknown = o[k]; //Type 'T[keyof T]' is not assignable to type 'unknown'.
} Is there something T[keyof T] could be that isn't assignable to {} | null | undefined ? |
that is a special case really. the general case is that you can not make assumptions about |
Before 2.7 that assignment worked, although maybe it shouldn't have. I have found a workaround using mapped types, however: type unknown = {} | null | undefined;
function fn<K extends string, T extends {[I in K]: unknown}>(o: T, k: K) {
var v: unknown = o[k]; //Works!
} |
Please see my response in #21369 (comment) |
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. @RyanCavanaugh is working on a proposal here. it is going to be a new primitive type. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.7.0-dev.20180123
Search Terms: indexed access, type parameter keyof
Code
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.
Playground Link:
Related Issues:
#12991
The text was updated successfully, but these errors were encountered: