-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathexclusify-union.d.ts
More file actions
147 lines (121 loc) · 3.52 KB
/
exclusify-union.d.ts
File metadata and controls
147 lines (121 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type {If} from './if.d.ts';
import type {IfNotAnyOrNever, MapsSetsOrArrays, NonRecursiveType} from './internal/type.d.ts';
import type {IsUnknown} from './is-unknown.d.ts';
import type {KeysOfUnion} from './keys-of-union.d.ts';
import type {Simplify} from './simplify.d.ts';
/**
Ensure mutual exclusivity in object unions by adding other members’ keys as `?: never`.
Use-cases:
- You want each union member to be exclusive, preventing overlapping object shapes.
- You want to safely access any property defined across the union without additional type guards.
@example
```
import type {ExclusifyUnion} from 'type-fest';
type FileConfig = {
filePath: string;
};
type InlineConfig = {
content: string;
};
declare function loadConfig1(options: FileConfig | InlineConfig): void;
// Someone could mistakenly provide both `filePath` and `content`.
loadConfig1({filePath: './config.json', content: '{ "name": "app" }'}); // No errors
// Use `ExclusifyUnion` to prevent that mistake.
type Config = ExclusifyUnion<FileConfig | InlineConfig>;
//=> {
// filePath: string;
// content?: never;
// } | {
// content: string;
// filePath?: never;
// }
declare function loadConfig2(options: Config): void;
// @ts-expect-error
loadConfig2({filePath: './config.json', content: '{ "name": "app" }'});
// Error: Argument of type '{ filePath: string; content: string; }' is not assignable to parameter of type '{ filePath: string; content?: never; } | { content: string; filePath?: never; }'.
loadConfig2({filePath: './config.json'}); // Ok
loadConfig2({content: '{ "name": "app" }'}); // Ok
```
@example
```
import type {ExclusifyUnion} from 'type-fest';
type CardPayment = {
amount: number;
cardNumber: string;
};
type PaypalPayment = {
amount: number;
paypalId: string;
};
function processPayment1(payment: CardPayment | PaypalPayment) {
// @ts-expect-error
const details = payment.cardNumber ?? payment.paypalId; // Cannot access `cardNumber` or `paypalId` directly
}
type Payment = ExclusifyUnion<CardPayment | PaypalPayment>;
//=> {
// amount: number;
// cardNumber: string;
// paypalId?: never;
// } | {
// amount: number;
// paypalId: string;
// cardNumber?: never;
// }
function processPayment2(payment: Payment) {
const details = payment.cardNumber ?? payment.paypalId; // Ok
//=> string
}
```
@example
```
import type {ExclusifyUnion} from 'type-fest';
type A = ExclusifyUnion<{a: string} | {b: number}>;
//=> {a: string; b?: never} | {b: number; a?: never}
type B = ExclusifyUnion<{a: string} | {b: number} | {c: boolean}>;
//=> {
// a: string;
// b?: never;
// c?: never;
// } | {
// b: number;
// a?: never;
// c?: never;
// } | {
// c: boolean;
// a?: never;
// b?: never;
// }
type C = ExclusifyUnion<{a: string; b: number} | {b: string; c: number}>;
//=> {
// a: string;
// b: number;
// c?: never;
// } | {
// b: string;
// c: number;
// a?: never;
// }
type D = ExclusifyUnion<{a?: 1; readonly b: 2} | {d: 4}>;
//=> {a?: 1; readonly b: 2; d?: never} | {d: 4; a?: never; b?: never}
```
@category Object
@category Union
*/
export type ExclusifyUnion<Union> = IfNotAnyOrNever<Union,
If<IsUnknown<Union>, Union,
Extract<Union, NonRecursiveType | MapsSetsOrArrays> extends infer SkippedMembers
? SkippedMembers | _ExclusifyUnion<Exclude<Union, SkippedMembers>>
: never
>
>;
type _ExclusifyUnion<Union, UnionCopy = Union> = Union extends unknown // For distributing `Union`
? Simplify<
Union & Partial<
Record<
Exclude<KeysOfUnion<UnionCopy>, keyof Union>,
never
>
>
>
: never; // Should never happen
export {};