Last active
January 13, 2023 13:13
-
-
Save TorbjornHoltmon/554f23229bd98f8ef6e4075f42283e5a to your computer and use it in GitHub Desktop.
PartialBy, make properties in typescript type optional
This file contains hidden or 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
/** | |
* Construct a type with the properties of T and make the properties K optional. | |
*/ | |
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | |
// Example: | |
type Person = { | |
name: string; | |
age: number; | |
gender: "male" | "female"; | |
}; | |
type PersonWithOptionalAgeAndGender = PartialBy<Person, "age" | "gender">; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment