Created
July 9, 2022 17:46
-
-
Save joaobispo2077/e764e693ab088282b50f02e3cc9bb4d3 to your computer and use it in GitHub Desktop.
MakePartial Generic - Transform some keys from a Type into Optional Keys because built-in "Partial" Generic apply optional into all keys, but this only into few keys.
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
export type MakePartial< | |
OriginalType, | |
KeyFromOriginalType extends keyof OriginalType, | |
> = Omit<OriginalType, KeyFromOriginalType> & | |
Partial<Pick<OriginalType, KeyFromOriginalType>>; | |
/* Here is how to use: */ | |
// type User = { | |
// name: string; | |
// age: number; | |
// createdAt: Date; | |
// }; | |
// type PartialUser = MakePartial<User, 'age' | 'createdAt'>; | |
// const user: PartialUser = { | |
// name: 'John Doe', | |
// }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment