-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
naive type infer for key in number & keyof T #31575
Comments
type A = number & (1 | 2)
type B = number & keyof {1: any, 2: any} Both A and B is |
This is working as intended. Homomorphic mapped types (such as |
@ahejlsberg If it's much harder or proven to be impossible to deal with non-homomorphic mapped type same as with homomorphic one for some technical issues, then I don't have anything more to ask for. #31385 (comment), it sounds like that the current behavior is for less verbosity and keeping the old behavior, though we can claim more explicit constraints on And I read in #26063
Can If then, I want to introduce a possible use case of explicit narrowing with type Human = {
firstName: string;
lastName: string
age: number;
}
function queryHuman<T extends {readonly [key: number]: keyof Human} & {[key: string]: keyof Human}>
(fields: T): {[key in T[number & keyof T]]: key} & {[key in string & keyof T]: T[key]} {
return {} as any;
}
/*
human {
firstName
age
}
*/
queryHuman(["firstName", "age"] as const); // not working
/*
human {
a: firstName
firstName
lastName
}
*/
queryHuman({
a: "firstName",
firstName: "firstName", // verbose
lastName: "lastName", // verbose
}); // working
/*
human {
a: firstName
firstName
lastName
}
*/
queryHuman({
a: "firstName",
...["firstName", "lastName"] as const,
}); // not working It's not working because of the current behavior of To be honest, I can't strongly insist that this change can give a rich improvement in various domains. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.5.0-dev.20190523
Search Terms:
generic, keyof ,indexed access type
Code
Expected behavior:
B is the same type as A.
Actual behavior:
B is a naiver one.
Playground Link: https://www.typescriptlang.org/play/#src=type%20G1%3CT%20extends%20%7B%20%5Bkey%3A%20number%5D%3A%20any%20%7D%3E%20%3D%20%7B%20%5Bkey%20in%20keyof%20T%5D%3A%20T%5Bkey%5D%5B%5D%20%7D%3B%0D%0Atype%20G2%3CT%20extends%20%7B%20%5Bkey%3A%20number%5D%3A%20any%20%7D%3E%20%3D%20%7B%20%5Bkey%20in%20number%20%26%20keyof%20T%5D%3A%20T%5Bkey%5D%5B%5D%20%7D%3B%0D%0A%0D%0Atype%20A%20%3D%20G1%3C%5B%22a%22%2C%20%22b%22%5D%3E%3B%20%2F%2F%20%5Bnumber%5B%5D%2C%20string%5B%5D%5D%0D%0Atype%20B%20%3D%20G2%3C%5B%22a%22%2C%20%22b%22%5D%3E%3B%20%2F%2F%20%7B%20%5Bx%3Anumber%5D%3A%20(number%20%7C%20string)%5B%5D%20%7D
Related Issues:
#30769 maybe?
The text was updated successfully, but these errors were encountered: