UNPKG

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