1 | // Helper type. Not useful on its own.
|
2 | type Without<FirstType, SecondType> = {[KeyType in Exclude<keyof FirstType, keyof SecondType>]?: never};
|
3 |
|
4 | /**
|
5 | Create a type that has mutually exclusive properties.
|
6 |
|
7 | This type was inspired by [this comment](https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604).
|
8 |
|
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`.
|
10 |
|
11 | @example
|
12 | ```
|
13 | import {MergeExclusive} from 'type-fest';
|
14 |
|
15 | interface ExclusiveVariation1 {
|
16 | exclusive1: boolean;
|
17 | }
|
18 |
|
19 | interface ExclusiveVariation2 {
|
20 | exclusive2: string;
|
21 | }
|
22 |
|
23 | type ExclusiveOptions = MergeExclusive<ExclusiveVariation1, ExclusiveVariation2>;
|
24 |
|
25 | let exclusiveOptions: ExclusiveOptions;
|
26 |
|
27 | exclusiveOptions = {exclusive1: true};
|
28 | //=> Works
|
29 | exclusiveOptions = {exclusive2: 'hi'};
|
30 | //=> Works
|
31 | exclusiveOptions = {exclusive1: true, exclusive2: 'hi'};
|
32 | //=> Error
|
33 | ```
|
34 | */
|
35 | export type MergeExclusive<FirstType, SecondType> =
|
36 | (FirstType | SecondType) extends object ?
|
37 | (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
|
38 | FirstType | SecondType;
|
39 |
|