Created
October 7, 2022 08:43
-
-
Save semlinker/d3c43d76681474d631cb6201bd782742 to your computer and use it in GitHub Desktop.
Implement a generic PartialByKeys<T, K> which takes two type argument T and K.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface User { | |
name: string; | |
age: number; | |
address: string; | |
} | |
type PartialByKeys<T, K = keyof T> = Merge< | |
{ | |
[P in keyof T as P extends K ? P : never]?: T[P]; | |
} & { | |
[P in keyof T as P extends K ? never : P]: T[P]; | |
} | |
>; | |
type Merge<T> = { | |
[P in keyof T]: T[P]; | |
}; | |
type UserPartialName = PartialByKeys<User, "name">; | |
// { name?:string; age:number; address:string } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment