Skip to content

Instantly share code, notes, and snippets.

@ali-master
Created November 6, 2021 07:44
Show Gist options
  • Save ali-master/c4a72c2d4925bb53bbac88cc842b69ae to your computer and use it in GitHub Desktop.
Save ali-master/c4a72c2d4925bb53bbac88cc842b69ae to your computer and use it in GitHub Desktop.
RequireOnlyOne & PartialOnlyOne in typescript
type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]-?: Required<Pick<T, K>>;
}[Keys];
type PartialOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]?: Partial<Pick<T, K>>;
}[Keys];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment