Last active
December 20, 2018 02:33
-
-
Save christianscott/e93388085eaaa48586a10a76e7aa793c to your computer and use it in GitHub Desktop.
Make properties on an existing 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
type OptionalProperty<T, P extends keyof T> = { | |
[K in Exclude<keyof T, P>]: T[K]; | |
} & { | |
[K in Extract<keyof T, P>]?: T[K]; | |
}; | |
// Example | |
type Person = { age: number; name: string; country: string; }; | |
const person: OptionalProperty<Person, 'age' | 'country'> = { name: 'Person', age: undefined }; // OK | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment