Skip to content

Instantly share code, notes, and snippets.

@ycmjason
Created September 21, 2024 12:10
Show Gist options
  • Save ycmjason/164bbcc8685fa457400272a96ec61451 to your computer and use it in GitHub Desktop.
Save ycmjason/164bbcc8685fa457400272a96ec61451 to your computer and use it in GitHub Desktop.

This type would evaluate to union of "exclusive" shapes.

For example, the following type

type A = ExclusiveUnion<{a: number} | {b: string}>;

Evaluates to

type A = { a: number, b?: never } | { a?: never, b: string }
type KeysOfUnion<T> = T extends T ? keyof T : never;
export type ExclusiveUnion<T> = KeysOfUnion<T> extends
infer K extends PropertyKey
? T extends infer A ? A & Partial<Record<Exclude<K, keyof A>, never>>
: never
: never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment