Skip to content

Instantly share code, notes, and snippets.

@forivall
Last active February 23, 2024 20:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forivall/26e1af05175b0c1c95d0b3d98f2a1121 to your computer and use it in GitHub Desktop.
Save forivall/26e1af05175b0c1c95d0b3d98f2a1121 to your computer and use it in GitHub Desktop.
ExclusifyUnion improved goto definiion
import { UnionToIntersection } from 'type-fest';
export type ExclusifyUnion<T> = ExclusifyUnion_<T, T>;
type ExclusifyUnion_<T, U> = T extends never
? never
: T & EmptyDifference<T, Exclude<U, T>>; // eslint-disable-line @typescript-eslint/sort-type-constituents
type EmptyDifference<T, U> = Omit<
UnionToIntersection<T extends never ? never : { [K in keyof U]?: never }>,
keyof T
>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment