Type alias ExclusiveUnion<T, AllKeys>

ExclusiveUnion<T, AllKeys>: T extends unknown
    ? _ExclusiveUnionMember<T, AllKeys>
    : never

Constructs a strict exclusive union from a union of object types.

Each member keeps its own properties unchanged, while properties that belong to other union members are added as optional never fields.

This makes the union mutually exclusive at the type level and is especially useful for configuration objects, variant props, and discriminated unions.

Type Parameters

  • T extends object

    The union of object types to make exclusive.

  • AllKeys extends PropertyKey = _KeysOfUnion<T>

    The full key set across all members of T.

Example

type Config = ExclusiveUnion<
| { dbConnectionString: string; maxConnections: number }
| { apiEndpoint: string; apiKey: string }
>;

// Result:
// | {
// dbConnectionString: string;
// maxConnections: number;
// apiEndpoint?: never;
// apiKey?: never;
// }
// | {
// dbConnectionString?: never;
// maxConnections?: never;
// apiEndpoint: string;
// apiKey: string;
// }