-
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
Properties P in generic type U that extends type T should allow use of type T[P] #13547
Comments
type shouldCompile<T, U extends T> = {
[P in keyof T]: U[P];
} |
You are correct. For the real world scenario, I was thinking about Is there anything that would specify that Edit: Edit2: |
If there was something which produced only common properties between two types. type shouldCompile<T, U extends (T @ U)> = {
[P in keyof U]: T[P];
} |
I think the problem is with the word That's why I think there should be a whole separate keyword. For example, |
hey, @mhegazy, I understand that the word |
As described by @RyanCavanaugh in #13547 (comment), it is not guaranteed that |
I'm trying to have TypeScript recognize the return type of the function given its parameters. interface IPerson { id: string; name: string; age: number }
let person = db.getPerson<IPerson>({ id: 1, name: 1 });
// typescript should recognize the type of `person` is `{ id: string; name: string }` - without age property Here is my best shot at this example in TypeScript playground. |
I am assuming the object literal has getPerson<K extends keyof IPerson>(obj: Record<K, number>): Record<K, IPerson>; |
yes, type for properties of returning { id: IPerson; name: IPerson} instead of what's needed: { id: string; name: string } |
Sorry. In my brain I was thinking of declare function getPerson<K extends keyof IPerson>(obj: Record<K, number>): Pick<IPerson, K>; |
this is fantastic! Thanks for the help! That alone made my day! |
TypeScript Version: 2.1.4 / Playground
Code
Expected behavior:
Should compile because U extends T, thus has the same properties
Actual behavior:
Does not compile.
Additional comments
I think I saw something similar beeing discussed in one of the previous issues, but I could not find it. Sorry if this is a duplicate.
The type is kept simple for the purposes of demonstration of the issue. It's unlikely it'd be created like that. In a more real situation
U
would be extending aPartial<T>
or some other variation ofT
Also, the title of this issue proves once again that naming things is hard!
The text was updated successfully, but these errors were encountered: