Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 7, 2022 08:43
Show Gist options
  • Save semlinker/d3c43d76681474d631cb6201bd782742 to your computer and use it in GitHub Desktop.
Save semlinker/d3c43d76681474d631cb6201bd782742 to your computer and use it in GitHub Desktop.
Implement a generic PartialByKeys<T, K> which takes two type argument T and K.
interface User {
name: string;
age: number;
address: string;
}
type PartialByKeys<T, K = keyof T> = Merge<
{
[P in keyof T as P extends K ? P : never]?: T[P];
} & {
[P in keyof T as P extends K ? never : P]: T[P];
}
>;
type Merge<T> = {
[P in keyof T]: T[P];
};
type UserPartialName = PartialByKeys<User, "name">;
// { name?:string; age:number; address:string }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment