1 // Helper type. Not useful on its own.
2 type Without<FirstType, SecondType> = {[KeyType in Exclude<keyof FirstType, keyof SecondType>]?: never};
5 Create a type that has mutually exclusive keys.
7 This type was inspired by [this comment](https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604).
9 This type works with a helper type, called `Without`. `Without<FirstType, SecondType>` produces a type that has only keys from `FirstType` which are not present on `SecondType` and sets the value type for these keys to `never`. This helper type is then used in `MergeExclusive` to remove keys from either `FirstType` or `SecondType`.
13 import {MergeExclusive} from 'type-fest';
15 interface ExclusiveVariation1 {
19 interface ExclusiveVariation2 {
23 type ExclusiveOptions = MergeExclusive<ExclusiveVariation1, ExclusiveVariation2>;
25 let exclusiveOptions: ExclusiveOptions;
27 exclusiveOptions = {exclusive1: true};
29 exclusiveOptions = {exclusive2: 'hi'};
31 exclusiveOptions = {exclusive1: true, exclusive2: 'hi'};
35 export type MergeExclusive<FirstType, SecondType> =
36 (FirstType | SecondType) extends object ?
37 (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
38 FirstType | SecondType;