Skip to content

Instantly share code, notes, and snippets.

@TorbjornHoltmon
Last active January 13, 2023 13:13
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 TorbjornHoltmon/554f23229bd98f8ef6e4075f42283e5a to your computer and use it in GitHub Desktop.
Save TorbjornHoltmon/554f23229bd98f8ef6e4075f42283e5a to your computer and use it in GitHub Desktop.
PartialBy, make properties in typescript type optional
/**
* 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