source: trip-planner-front/node_modules/type-fest/source/merge-exclusive.d.ts@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
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 {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*/
35export type MergeExclusive<FirstType, SecondType> =
36 (FirstType | SecondType) extends object ?
37 (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
38 FirstType | SecondType;
39
Note: See TracBrowser for help on using the repository browser.