Skip to content

Instantly share code, notes, and snippets.

@johan
Created May 14, 2024 03:03
Show Gist options
  • Save johan/dd216df62fc3ff75c2dace7054af5d84 to your computer and use it in GitHub Desktop.
Save johan/dd216df62fc3ff75c2dace7054af5d84 to your computer and use it in GitHub Desktop.
/**
* Given any object type T, produce a type that requires you to set ONE
* of its properties, while forbidding any of the others to also be set.
*/
export type RequiresEitherProperty<T extends {}, K extends keyof T = keyof T> = {
[P in K]: Record<P, T[P]> &
Partial<{
[Y in Exclude<K, P>]: never;
}>;
}[K];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment