Skip to content

Instantly share code, notes, and snippets.

@joaobispo2077
Created July 9, 2022 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaobispo2077/e764e693ab088282b50f02e3cc9bb4d3 to your computer and use it in GitHub Desktop.
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.
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