Skip to content

Instantly share code, notes, and snippets.

@christianscott
Last active December 20, 2018 02:33
Show Gist options
  • Save christianscott/e93388085eaaa48586a10a76e7aa793c to your computer and use it in GitHub Desktop.
Save christianscott/e93388085eaaa48586a10a76e7aa793c to your computer and use it in GitHub Desktop.
Make properties on an existing type optional
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