-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 in 3.1+: mapped type cannot be used to index type #27413
Comments
interface Table<T> {
select<U extends {[K: string]: keyof T}>
(fields: U): {[K in keyof U]: T[U[K]]}
}
declare var t: Table<{x: string}>;
declare const s: unique symbol;
// your definition of `U` allows symbolic keys of `U` to have non-`keyof T` types,
// which in turn means a call like this would typecheck. `T` indexed by one of these
// types wouldn't exist, which is why we issue this error
const result = t.select({[s]: "not a key", member: "x"});
// `result` above will end up of type `{[s]: unknown, member: string}`, since your type doesn't
// actually handle the `symbol` key case I assume, anyway. I actually tried what I think would be the correct version that fixes that, though: interface Table<T> {
select<U extends {[K: string]: keyof T}>
(fields: U): {[K in Extract<keyof U, string>]: T[U[K]]}
} and had the same issue; so something else must be the issue. In any case, you can work around the problem by writing your type like so: interface Table<T> {
select<U extends {[K: string]: keyof T}>
(fields: U): {[K in Extract<keyof U, string>]: T[U[K] & keyof T]}
} (I left in the |
Thanks, good call on the workaround. U[K] used to be implicitly constrained to keyof T. I'm not sure exactly when the behaviour changed. |
Maybe related:
This shows the error: |
Closing this since it looks like the original issue is fixed. |
TypeScript Version: 3.1.1 and 3.2.0-dev.20180927
Search Terms:
mapped type index
Code
Expected behavior:
U[K] is keyof T and so T[U[K]] is computed successfully
Actual behavior:
Gives the error "Type 'U[K]' cannot be used to index type 'T'".
The same code compiles with tsc 3.0.3 and the return type gets computed correctly.
The text was updated successfully, but these errors were encountered: