@storybook/react 10.5.0-alpha.1 → 10.5.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +770 -163
- package/dist/preset.js +6 -6
- package/dist/preview.d.ts +770 -163
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -4,99 +4,390 @@ export { ArgTypes, Args, Parameters, StrictArgs } from 'storybook/internal/types
|
|
|
4
4
|
import { RootOptions } from 'react-dom/client';
|
|
5
5
|
import { PreviewAddon, InferTypes, AddonTypes, Preview as Preview$1, Meta as Meta$1, Story } from 'storybook/internal/csf';
|
|
6
6
|
|
|
7
|
-
declare global {
|
|
8
|
-
interface SymbolConstructor {
|
|
9
|
-
readonly observable: symbol;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
7
|
/**
|
|
14
|
-
|
|
8
|
+
Convert a union type to an intersection type.
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
11
|
+
|
|
12
|
+
@example
|
|
13
|
+
```
|
|
14
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
15
|
+
|
|
16
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
17
|
+
|
|
18
|
+
type Intersection = UnionToIntersection<Union>;
|
|
19
|
+
//=> {the(): void} & {great(arg: string): void} & {escape: boolean}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
@category Type
|
|
18
23
|
*/
|
|
19
|
-
type
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
type UnionToIntersection<Union> = (
|
|
25
|
+
// `extends unknown` is always going to be the case and is used to convert the
|
|
26
|
+
// `Union` into a [distributive conditional
|
|
27
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
28
|
+
Union extends unknown
|
|
29
|
+
// The union type is used as the only argument to a function since the union
|
|
30
|
+
// of function arguments is an intersection.
|
|
31
|
+
? (distributedUnion: Union) => void
|
|
32
|
+
// This won't happen.
|
|
33
|
+
: never
|
|
34
|
+
// Infer the `Intersection` type since TypeScript represents the positional
|
|
35
|
+
// arguments of unions of functions as an intersection of the union.
|
|
36
|
+
) extends ((mergedIntersection: infer Intersection) => void)
|
|
37
|
+
// The `& Union` is to ensure result of `UnionToIntersection<A | B>` is always assignable to `A | B`
|
|
38
|
+
? Intersection & Union
|
|
39
|
+
: never;
|
|
24
40
|
|
|
25
41
|
/**
|
|
26
|
-
|
|
42
|
+
Create a union of all keys from a given type, even those exclusive to specific union members.
|
|
27
43
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
44
|
+
Unlike the native `keyof` keyword, which returns keys present in **all** union members, this type returns keys from **any** member.
|
|
45
|
+
|
|
46
|
+
@link https://stackoverflow.com/a/49402091
|
|
31
47
|
|
|
32
48
|
@example
|
|
33
49
|
```
|
|
34
|
-
type
|
|
35
|
-
|
|
50
|
+
import type {KeysOfUnion} from 'type-fest';
|
|
51
|
+
|
|
52
|
+
type A = {
|
|
53
|
+
common: string;
|
|
54
|
+
a: number;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type B = {
|
|
58
|
+
common: string;
|
|
59
|
+
b: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
type C = {
|
|
63
|
+
common: string;
|
|
64
|
+
c: boolean;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type Union = A | B | C;
|
|
68
|
+
|
|
69
|
+
type CommonKeys = keyof Union;
|
|
70
|
+
//=> 'common'
|
|
71
|
+
|
|
72
|
+
type AllKeys = KeysOfUnion<Union>;
|
|
73
|
+
//=> 'common' | 'a' | 'b' | 'c'
|
|
36
74
|
```
|
|
37
75
|
|
|
76
|
+
@category Object
|
|
77
|
+
*/
|
|
78
|
+
type KeysOfUnion<ObjectType> =
|
|
79
|
+
// Hack to fix https://github.com/sindresorhus/type-fest/issues/1008
|
|
80
|
+
keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
Returns a boolean for whether the given type is `any`.
|
|
84
|
+
|
|
85
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
86
|
+
|
|
87
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
88
|
+
|
|
38
89
|
@example
|
|
39
90
|
```
|
|
40
|
-
type
|
|
41
|
-
|
|
91
|
+
import type {IsAny} from 'type-fest';
|
|
92
|
+
|
|
93
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
94
|
+
const anyObject: any = {a: 1, b: 2};
|
|
95
|
+
|
|
96
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
|
|
97
|
+
return object[key];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const typedA = get(typedObject, 'a');
|
|
101
|
+
//=> 1
|
|
102
|
+
|
|
103
|
+
const anyA = get(anyObject, 'a');
|
|
104
|
+
//=> any
|
|
42
105
|
```
|
|
43
106
|
|
|
107
|
+
@category Type Guard
|
|
108
|
+
@category Utilities
|
|
109
|
+
*/
|
|
110
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
Returns a boolean for whether the given key is an optional key of type.
|
|
114
|
+
|
|
115
|
+
This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
|
|
116
|
+
|
|
44
117
|
@example
|
|
45
118
|
```
|
|
46
|
-
type
|
|
47
|
-
|
|
119
|
+
import type {IsOptionalKeyOf} from 'type-fest';
|
|
120
|
+
|
|
121
|
+
type User = {
|
|
122
|
+
name: string;
|
|
123
|
+
surname: string;
|
|
124
|
+
|
|
125
|
+
luckyNumber?: number;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
type Admin = {
|
|
129
|
+
name: string;
|
|
130
|
+
surname?: string;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
134
|
+
//=> true
|
|
135
|
+
|
|
136
|
+
type T2 = IsOptionalKeyOf<User, 'name'>;
|
|
137
|
+
//=> false
|
|
138
|
+
|
|
139
|
+
type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
|
|
140
|
+
//=> boolean
|
|
141
|
+
|
|
142
|
+
type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
|
|
143
|
+
//=> false
|
|
144
|
+
|
|
145
|
+
type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
|
|
146
|
+
//=> boolean
|
|
48
147
|
```
|
|
49
148
|
|
|
50
|
-
@
|
|
149
|
+
@category Type Guard
|
|
150
|
+
@category Utilities
|
|
51
151
|
*/
|
|
52
|
-
type
|
|
152
|
+
type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
|
|
153
|
+
IsAny<Type | Key> extends true ? never
|
|
154
|
+
: Key extends keyof Type
|
|
155
|
+
? Type extends Record<Key, Type[Key]>
|
|
156
|
+
? false
|
|
157
|
+
: true
|
|
158
|
+
: false;
|
|
53
159
|
|
|
54
160
|
/**
|
|
55
|
-
|
|
161
|
+
Extract all optional keys from the given type.
|
|
56
162
|
|
|
57
|
-
This
|
|
58
|
-
|
|
59
|
-
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
|
|
163
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
60
164
|
|
|
61
165
|
@example
|
|
62
166
|
```
|
|
63
|
-
import type {Except} from 'type-fest';
|
|
167
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
64
168
|
|
|
65
|
-
type
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
169
|
+
type User = {
|
|
170
|
+
name: string;
|
|
171
|
+
surname: string;
|
|
172
|
+
|
|
173
|
+
luckyNumber?: number;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
177
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
178
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
69
179
|
};
|
|
70
180
|
|
|
71
|
-
|
|
72
|
-
|
|
181
|
+
const update1: UpdateOperation<User> = {
|
|
182
|
+
name: 'Alice',
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const update2: UpdateOperation<User> = {
|
|
186
|
+
name: 'Bob',
|
|
187
|
+
luckyNumber: REMOVE_FIELD,
|
|
188
|
+
};
|
|
73
189
|
```
|
|
74
190
|
|
|
75
|
-
@category
|
|
191
|
+
@category Utilities
|
|
76
192
|
*/
|
|
77
|
-
type
|
|
78
|
-
|
|
193
|
+
type OptionalKeysOf<Type extends object> =
|
|
194
|
+
Type extends unknown // For distributing `Type`
|
|
195
|
+
? (keyof {[Key in keyof Type as
|
|
196
|
+
IsOptionalKeyOf<Type, Key> extends false
|
|
197
|
+
? never
|
|
198
|
+
: Key
|
|
199
|
+
]: never
|
|
200
|
+
}) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
|
|
201
|
+
: never; // Should never happen
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
Extract all required keys from the given type.
|
|
205
|
+
|
|
206
|
+
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
207
|
+
|
|
208
|
+
@example
|
|
209
|
+
```
|
|
210
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
211
|
+
|
|
212
|
+
declare function createValidation<
|
|
213
|
+
Entity extends object,
|
|
214
|
+
Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
|
|
215
|
+
>(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
|
|
216
|
+
|
|
217
|
+
type User = {
|
|
218
|
+
name: string;
|
|
219
|
+
surname: string;
|
|
220
|
+
luckyNumber?: number;
|
|
79
221
|
};
|
|
80
222
|
|
|
223
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
224
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
225
|
+
|
|
226
|
+
// @ts-expect-error
|
|
227
|
+
const validator3 = createValidation<User>('luckyNumber', value => value > 0);
|
|
228
|
+
// Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
@category Utilities
|
|
232
|
+
*/
|
|
233
|
+
type RequiredKeysOf<Type extends object> =
|
|
234
|
+
Type extends unknown // For distributing `Type`
|
|
235
|
+
? Exclude<keyof Type, OptionalKeysOf<Type>>
|
|
236
|
+
: never; // Should never happen
|
|
237
|
+
|
|
81
238
|
/**
|
|
82
|
-
|
|
239
|
+
Returns a boolean for whether the given type is `never`.
|
|
240
|
+
|
|
241
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
242
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
243
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
244
|
+
|
|
245
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
246
|
+
|
|
247
|
+
@example
|
|
248
|
+
```
|
|
249
|
+
import type {IsNever, And} from 'type-fest';
|
|
250
|
+
|
|
251
|
+
type A = IsNever<never>;
|
|
252
|
+
//=> true
|
|
253
|
+
|
|
254
|
+
type B = IsNever<any>;
|
|
255
|
+
//=> false
|
|
256
|
+
|
|
257
|
+
type C = IsNever<unknown>;
|
|
258
|
+
//=> false
|
|
259
|
+
|
|
260
|
+
type D = IsNever<never[]>;
|
|
261
|
+
//=> false
|
|
262
|
+
|
|
263
|
+
type E = IsNever<object>;
|
|
264
|
+
//=> false
|
|
265
|
+
|
|
266
|
+
type F = IsNever<string>;
|
|
267
|
+
//=> false
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
@example
|
|
271
|
+
```
|
|
272
|
+
import type {IsNever} from 'type-fest';
|
|
273
|
+
|
|
274
|
+
type IsTrue<T> = T extends true ? true : false;
|
|
275
|
+
|
|
276
|
+
// When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
|
|
277
|
+
type A = IsTrue<never>;
|
|
278
|
+
//=> never
|
|
279
|
+
|
|
280
|
+
// If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
|
|
281
|
+
type IsTrueFixed<T> =
|
|
282
|
+
IsNever<T> extends true ? false : T extends true ? true : false;
|
|
283
|
+
|
|
284
|
+
type B = IsTrueFixed<never>;
|
|
285
|
+
//=> false
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
@category Type Guard
|
|
289
|
+
@category Utilities
|
|
83
290
|
*/
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
Do the simplification recursively.
|
|
291
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
87
292
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
deep?: boolean;
|
|
91
|
-
}
|
|
293
|
+
/**
|
|
294
|
+
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
92
295
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
296
|
+
Use-cases:
|
|
297
|
+
- You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
|
|
298
|
+
|
|
299
|
+
Note:
|
|
300
|
+
- Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
|
|
301
|
+
- Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
|
|
302
|
+
|
|
303
|
+
@example
|
|
304
|
+
```
|
|
305
|
+
import type {If} from 'type-fest';
|
|
306
|
+
|
|
307
|
+
type A = If<true, 'yes', 'no'>;
|
|
308
|
+
//=> 'yes'
|
|
309
|
+
|
|
310
|
+
type B = If<false, 'yes', 'no'>;
|
|
311
|
+
//=> 'no'
|
|
312
|
+
|
|
313
|
+
type C = If<boolean, 'yes', 'no'>;
|
|
314
|
+
//=> 'yes' | 'no'
|
|
315
|
+
|
|
316
|
+
type D = If<any, 'yes', 'no'>;
|
|
317
|
+
//=> 'yes' | 'no'
|
|
318
|
+
|
|
319
|
+
type E = If<never, 'yes', 'no'>;
|
|
320
|
+
//=> 'no'
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
@example
|
|
324
|
+
```
|
|
325
|
+
import type {If, IsAny, IsNever} from 'type-fest';
|
|
326
|
+
|
|
327
|
+
type A = If<IsAny<unknown>, 'is any', 'not any'>;
|
|
328
|
+
//=> 'not any'
|
|
329
|
+
|
|
330
|
+
type B = If<IsNever<never>, 'is never', 'not never'>;
|
|
331
|
+
//=> 'is never'
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
@example
|
|
335
|
+
```
|
|
336
|
+
import type {If, IsEqual} from 'type-fest';
|
|
337
|
+
|
|
338
|
+
type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
|
|
339
|
+
|
|
340
|
+
type A = IfEqual<string, string, 'equal', 'not equal'>;
|
|
341
|
+
//=> 'equal'
|
|
342
|
+
|
|
343
|
+
type B = IfEqual<string, number, 'equal', 'not equal'>;
|
|
344
|
+
//=> 'not equal'
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
Note: Sometimes using the `If` type can make an implementation non–tail-recursive, which can impact performance. In such cases, it’s better to use a conditional directly. Refer to the following example:
|
|
348
|
+
|
|
349
|
+
@example
|
|
350
|
+
```
|
|
351
|
+
import type {If, IsEqual, StringRepeat} from 'type-fest';
|
|
352
|
+
|
|
353
|
+
type HundredZeroes = StringRepeat<'0', 100>;
|
|
354
|
+
|
|
355
|
+
// The following implementation is not tail recursive
|
|
356
|
+
type Includes<S extends string, Char extends string> =
|
|
357
|
+
S extends `${infer First}${infer Rest}`
|
|
358
|
+
? If<IsEqual<First, Char>,
|
|
359
|
+
'found',
|
|
360
|
+
Includes<Rest, Char>>
|
|
361
|
+
: 'not found';
|
|
362
|
+
|
|
363
|
+
// Hence, instantiations with long strings will fail
|
|
364
|
+
// @ts-expect-error
|
|
365
|
+
type Fails = Includes<HundredZeroes, '1'>;
|
|
366
|
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
367
|
+
// Error: Type instantiation is excessively deep and possibly infinite.
|
|
368
|
+
|
|
369
|
+
// However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive
|
|
370
|
+
type IncludesWithoutIf<S extends string, Char extends string> =
|
|
371
|
+
S extends `${infer First}${infer Rest}`
|
|
372
|
+
? IsEqual<First, Char> extends true
|
|
373
|
+
? 'found'
|
|
374
|
+
: IncludesWithoutIf<Rest, Char>
|
|
375
|
+
: 'not found';
|
|
376
|
+
|
|
377
|
+
// Now, instantiations with long strings will work
|
|
378
|
+
type Works = IncludesWithoutIf<HundredZeroes, '1'>;
|
|
379
|
+
//=> 'not found'
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
@category Type Guard
|
|
383
|
+
@category Utilities
|
|
384
|
+
*/
|
|
385
|
+
type If<Type extends boolean, IfBranch, ElseBranch> =
|
|
386
|
+
IsNever<Type> extends true
|
|
387
|
+
? ElseBranch
|
|
388
|
+
: Type extends true
|
|
389
|
+
? IfBranch
|
|
390
|
+
: ElseBranch;
|
|
100
391
|
|
|
101
392
|
/**
|
|
102
393
|
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
@@ -143,27 +434,65 @@ const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
|
143
434
|
const someType: SomeType = literal;
|
|
144
435
|
const someInterface: SomeInterface = literal;
|
|
145
436
|
|
|
146
|
-
function fn(object: Record<string, unknown>): void
|
|
437
|
+
declare function fn(object: Record<string, unknown>): void;
|
|
147
438
|
|
|
148
439
|
fn(literal); // Good: literal object type is sealed
|
|
149
440
|
fn(someType); // Good: type is sealed
|
|
441
|
+
// @ts-expect-error
|
|
150
442
|
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
151
443
|
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
152
444
|
```
|
|
153
445
|
|
|
154
446
|
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
155
|
-
|
|
447
|
+
@see {@link SimplifyDeep}
|
|
156
448
|
@category Object
|
|
157
449
|
*/
|
|
158
|
-
type Simplify<
|
|
159
|
-
AnyType,
|
|
160
|
-
Options extends SimplifyOptions = {},
|
|
161
|
-
> = Flatten<AnyType> extends AnyType
|
|
162
|
-
? Flatten<AnyType, Options>
|
|
163
|
-
: AnyType;
|
|
450
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
164
451
|
|
|
165
452
|
/**
|
|
166
|
-
|
|
453
|
+
Returns a boolean for whether the two given types are equal.
|
|
454
|
+
|
|
455
|
+
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
456
|
+
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
457
|
+
|
|
458
|
+
Use-cases:
|
|
459
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
460
|
+
|
|
461
|
+
@example
|
|
462
|
+
```
|
|
463
|
+
import type {IsEqual} from 'type-fest';
|
|
464
|
+
|
|
465
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
466
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
467
|
+
type Includes<Value extends readonly any[], Item> =
|
|
468
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
469
|
+
? IsEqual<Value[0], Item> extends true
|
|
470
|
+
? true
|
|
471
|
+
: Includes<rest, Item>
|
|
472
|
+
: false;
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
@category Type Guard
|
|
476
|
+
@category Utilities
|
|
477
|
+
*/
|
|
478
|
+
type IsEqual<A, B> =
|
|
479
|
+
[A] extends [B]
|
|
480
|
+
? [B] extends [A]
|
|
481
|
+
? _IsEqual<A, B>
|
|
482
|
+
: false
|
|
483
|
+
: false;
|
|
484
|
+
|
|
485
|
+
// This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
|
|
486
|
+
type _IsEqual<A, B> =
|
|
487
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
488
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
489
|
+
? true
|
|
490
|
+
: false;
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
494
|
+
|
|
495
|
+
This is the counterpart of `PickIndexSignature`.
|
|
167
496
|
|
|
168
497
|
Use-cases:
|
|
169
498
|
- Remove overly permissive signatures from third-party types.
|
|
@@ -177,8 +506,9 @@ It relies on the fact that an empty object (`{}`) is assignable to an object wit
|
|
|
177
506
|
```
|
|
178
507
|
const indexed: Record<string, unknown> = {}; // Allowed
|
|
179
508
|
|
|
509
|
+
// @ts-expect-error
|
|
180
510
|
const keyed: Record<'foo', unknown> = {}; // Error
|
|
181
|
-
//
|
|
511
|
+
// TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
182
512
|
```
|
|
183
513
|
|
|
184
514
|
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
@@ -187,178 +517,455 @@ Instead of causing a type error like the above, you can also use a [conditional
|
|
|
187
517
|
type Indexed = {} extends Record<string, unknown>
|
|
188
518
|
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
189
519
|
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
190
|
-
|
|
520
|
+
|
|
521
|
+
type IndexedResult = Indexed;
|
|
522
|
+
//=> '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
191
523
|
|
|
192
524
|
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
193
|
-
?
|
|
194
|
-
:
|
|
195
|
-
|
|
525
|
+
? '✅ `{}` is assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
526
|
+
: '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`';
|
|
527
|
+
|
|
528
|
+
type KeyedResult = Keyed;
|
|
529
|
+
//=> '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
196
530
|
```
|
|
197
531
|
|
|
198
532
|
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
199
533
|
|
|
200
534
|
```
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
535
|
+
type OmitIndexSignature<ObjectType> = {
|
|
204
536
|
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
205
|
-
]: ObjectType[KeyType]; // ...to its original value, i.e. `
|
|
537
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
206
538
|
};
|
|
207
539
|
```
|
|
208
540
|
|
|
209
541
|
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
210
542
|
|
|
211
543
|
```
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
544
|
+
type OmitIndexSignature<ObjectType> = {
|
|
215
545
|
[KeyType in keyof ObjectType
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
546
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
547
|
+
as {} extends Record<KeyType, unknown>
|
|
548
|
+
? never // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
549
|
+
: KeyType // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
220
550
|
]: ObjectType[KeyType];
|
|
221
551
|
};
|
|
222
552
|
```
|
|
223
553
|
|
|
224
554
|
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
225
555
|
|
|
226
|
-
```
|
|
227
|
-
import type {RemoveIndexSignature} from 'type-fest';
|
|
228
|
-
|
|
229
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
230
|
-
[KeyType in keyof ObjectType
|
|
231
|
-
as {} extends Record<KeyType, unknown>
|
|
232
|
-
? never // => Remove this `KeyType`.
|
|
233
|
-
: KeyType // => Keep this `KeyType` as it is.
|
|
234
|
-
]: ObjectType[KeyType];
|
|
235
|
-
};
|
|
236
|
-
```
|
|
237
|
-
|
|
238
556
|
@example
|
|
239
557
|
```
|
|
240
|
-
import type {
|
|
558
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
241
559
|
|
|
242
|
-
|
|
560
|
+
type Example = {
|
|
243
561
|
// These index signatures will be removed.
|
|
244
|
-
[x: string]: any
|
|
245
|
-
[x: number]: any
|
|
246
|
-
[x: symbol]: any
|
|
247
|
-
[x: `head-${string}`]: string
|
|
248
|
-
[x: `${string}-tail`]: string
|
|
249
|
-
[x: `head-${string}-tail`]: string
|
|
250
|
-
[x: `${bigint}`]: string
|
|
251
|
-
[x: `embedded-${number}`]: string
|
|
562
|
+
[x: string]: any;
|
|
563
|
+
[x: number]: any;
|
|
564
|
+
[x: symbol]: any;
|
|
565
|
+
[x: `head-${string}`]: string;
|
|
566
|
+
[x: `${string}-tail`]: string;
|
|
567
|
+
[x: `head-${string}-tail`]: string;
|
|
568
|
+
[x: `${bigint}`]: string;
|
|
569
|
+
[x: `embedded-${number}`]: string;
|
|
252
570
|
|
|
253
571
|
// These explicitly defined keys will remain.
|
|
254
572
|
foo: 'bar';
|
|
255
573
|
qux?: 'baz';
|
|
256
|
-
}
|
|
574
|
+
};
|
|
257
575
|
|
|
258
|
-
type ExampleWithoutIndexSignatures =
|
|
259
|
-
|
|
576
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
577
|
+
//=> {foo: 'bar'; qux?: 'baz'}
|
|
260
578
|
```
|
|
261
579
|
|
|
580
|
+
@see {@link PickIndexSignature}
|
|
262
581
|
@category Object
|
|
263
582
|
*/
|
|
264
|
-
type
|
|
583
|
+
type OmitIndexSignature<ObjectType> = {
|
|
265
584
|
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
266
585
|
? never
|
|
267
586
|
: KeyType]: ObjectType[KeyType];
|
|
268
587
|
};
|
|
269
588
|
|
|
270
589
|
/**
|
|
271
|
-
|
|
590
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
272
591
|
|
|
273
|
-
|
|
592
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
274
593
|
|
|
275
594
|
@example
|
|
276
595
|
```
|
|
277
|
-
import type {
|
|
596
|
+
import type {PickIndexSignature} from 'type-fest';
|
|
597
|
+
|
|
598
|
+
declare const symbolKey: unique symbol;
|
|
599
|
+
|
|
600
|
+
type Example = {
|
|
601
|
+
// These index signatures will remain.
|
|
602
|
+
[x: string]: unknown;
|
|
603
|
+
[x: number]: unknown;
|
|
604
|
+
[x: symbol]: unknown;
|
|
605
|
+
[x: `head-${string}`]: string;
|
|
606
|
+
[x: `${string}-tail`]: string;
|
|
607
|
+
[x: `head-${string}-tail`]: string;
|
|
608
|
+
[x: `${bigint}`]: string;
|
|
609
|
+
[x: `embedded-${number}`]: string;
|
|
610
|
+
|
|
611
|
+
// These explicitly defined keys will be removed.
|
|
612
|
+
['kebab-case-key']: string;
|
|
613
|
+
[symbolKey]: string;
|
|
614
|
+
foo: 'bar';
|
|
615
|
+
qux?: 'baz';
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
619
|
+
// {
|
|
620
|
+
// [x: string]: unknown;
|
|
621
|
+
// [x: number]: unknown;
|
|
622
|
+
// [x: symbol]: unknown;
|
|
623
|
+
// [x: `head-${string}`]: string;
|
|
624
|
+
// [x: `${string}-tail`]: string;
|
|
625
|
+
// [x: `head-${string}-tail`]: string;
|
|
626
|
+
// [x: `${bigint}`]: string;
|
|
627
|
+
// [x: `embedded-${number}`]: string;
|
|
628
|
+
// }
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
@see {@link OmitIndexSignature}
|
|
632
|
+
@category Object
|
|
633
|
+
*/
|
|
634
|
+
type PickIndexSignature<ObjectType> = {
|
|
635
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
636
|
+
? KeyType
|
|
637
|
+
: never]: ObjectType[KeyType];
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
// Merges two objects without worrying about index signatures.
|
|
641
|
+
type SimpleMerge<Destination, Source> = Simplify<{
|
|
642
|
+
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
643
|
+
} & Source>;
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
647
|
+
|
|
648
|
+
This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
|
|
649
|
+
|
|
650
|
+
@example
|
|
651
|
+
```
|
|
652
|
+
import type {Merge} from 'type-fest';
|
|
278
653
|
|
|
279
654
|
type Foo = {
|
|
280
|
-
a:
|
|
281
|
-
b
|
|
655
|
+
a: string;
|
|
656
|
+
b: number;
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
type Bar = {
|
|
660
|
+
a: number; // Conflicts with Foo['a']
|
|
282
661
|
c: boolean;
|
|
283
|
-
}
|
|
662
|
+
};
|
|
284
663
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
//
|
|
664
|
+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
|
|
665
|
+
type WithIntersection = (Foo & Bar)['a'];
|
|
666
|
+
//=> never
|
|
667
|
+
|
|
668
|
+
// With `Merge`, `a` is cleanly overridden to `number`.
|
|
669
|
+
type WithMerge = Merge<Foo, Bar>['a'];
|
|
670
|
+
//=> number
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
@example
|
|
674
|
+
```
|
|
675
|
+
import type {Merge} from 'type-fest';
|
|
676
|
+
|
|
677
|
+
type Foo = {
|
|
678
|
+
[x: string]: unknown;
|
|
679
|
+
[x: number]: unknown;
|
|
680
|
+
foo: string;
|
|
681
|
+
bar: symbol;
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
type Bar = {
|
|
685
|
+
[x: number]: number;
|
|
686
|
+
[x: symbol]: unknown;
|
|
687
|
+
bar: Date;
|
|
688
|
+
baz: boolean;
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
export type FooBar = Merge<Foo, Bar>;
|
|
692
|
+
//=> {
|
|
693
|
+
// [x: string]: unknown;
|
|
694
|
+
// [x: number]: number;
|
|
695
|
+
// [x: symbol]: unknown;
|
|
696
|
+
// foo: string;
|
|
697
|
+
// bar: Date;
|
|
698
|
+
// baz: boolean;
|
|
290
699
|
// }
|
|
291
700
|
```
|
|
292
701
|
|
|
702
|
+
Note: If you want a merge type that more accurately reflects the runtime behavior of object spread or `Object.assign`, refer to the {@link ObjectMerge} type.
|
|
703
|
+
|
|
704
|
+
@see {@link ObjectMerge}
|
|
293
705
|
@category Object
|
|
294
706
|
*/
|
|
295
|
-
type
|
|
707
|
+
type Merge<Destination, Source> =
|
|
708
|
+
Destination extends unknown // For distributing `Destination`
|
|
709
|
+
? Source extends unknown // For distributing `Source`
|
|
710
|
+
? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
|
|
711
|
+
: never // Should never happen
|
|
712
|
+
: never; // Should never happen
|
|
713
|
+
|
|
714
|
+
type _Merge<Destination, Source> =
|
|
296
715
|
Simplify<
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
300
|
-
Partial<Pick<BaseType, Keys>>
|
|
716
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
717
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
301
718
|
>;
|
|
302
719
|
|
|
303
720
|
/**
|
|
304
|
-
|
|
721
|
+
Works similar to the built-in `Pick` utility type, except for the following differences:
|
|
722
|
+
- Distributes over union types and allows picking keys from any member of the union type.
|
|
723
|
+
- Primitives types are returned as-is.
|
|
724
|
+
- Picks all keys if `Keys` is `any`.
|
|
725
|
+
- Doesn't pick `number` from a `string` index signature.
|
|
305
726
|
|
|
306
|
-
|
|
727
|
+
@example
|
|
728
|
+
```
|
|
729
|
+
type ImageUpload = {
|
|
730
|
+
url: string;
|
|
731
|
+
size: number;
|
|
732
|
+
thumbnailUrl: string;
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
type VideoUpload = {
|
|
736
|
+
url: string;
|
|
737
|
+
duration: number;
|
|
738
|
+
encodingFormat: string;
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
// Distributes over union types and allows picking keys from any member of the union type
|
|
742
|
+
type MediaDisplay = HomomorphicPick<ImageUpload | VideoUpload, "url" | "size" | "duration">;
|
|
743
|
+
//=> {url: string; size: number} | {url: string; duration: number}
|
|
744
|
+
|
|
745
|
+
// Primitive types are returned as-is
|
|
746
|
+
type Primitive = HomomorphicPick<string | number, 'toUpperCase' | 'toString'>;
|
|
747
|
+
//=> string | number
|
|
748
|
+
|
|
749
|
+
// Picks all keys if `Keys` is `any`
|
|
750
|
+
type Any = HomomorphicPick<{a: 1; b: 2} | {c: 3}, any>;
|
|
751
|
+
//=> {a: 1; b: 2} | {c: 3}
|
|
752
|
+
|
|
753
|
+
// Doesn't pick `number` from a `string` index signature
|
|
754
|
+
type IndexSignature = HomomorphicPick<{[k: string]: unknown}, number>;
|
|
755
|
+
//=> {}
|
|
756
|
+
*/
|
|
757
|
+
type HomomorphicPick<T, Keys extends KeysOfUnion<T>> = {
|
|
758
|
+
[P in keyof T as Extract<P, Keys>]: T[P]
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
Merges user specified options with default options.
|
|
307
763
|
|
|
308
764
|
@example
|
|
309
765
|
```
|
|
310
|
-
|
|
766
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
767
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
768
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
311
769
|
|
|
312
|
-
type
|
|
770
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
771
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
772
|
+
```
|
|
313
773
|
|
|
314
|
-
|
|
315
|
-
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
774
|
+
@example
|
|
316
775
|
```
|
|
776
|
+
// Complains if default values are not provided for optional options
|
|
317
777
|
|
|
318
|
-
|
|
778
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
779
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
780
|
+
type SpecifiedOptions = {};
|
|
781
|
+
|
|
782
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
783
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
784
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
785
|
+
```
|
|
319
786
|
|
|
320
787
|
@example
|
|
321
788
|
```
|
|
322
|
-
|
|
789
|
+
// Complains if an option's default type does not conform to the expected type
|
|
323
790
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
b1: () => undefined,
|
|
328
|
-
}
|
|
329
|
-
}
|
|
791
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
792
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
793
|
+
type SpecifiedOptions = {};
|
|
330
794
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
}
|
|
795
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
796
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
797
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
798
|
+
```
|
|
337
799
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
800
|
+
@example
|
|
801
|
+
```
|
|
802
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
341
803
|
|
|
342
|
-
type
|
|
343
|
-
|
|
804
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
805
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
806
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
807
|
+
|
|
808
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
809
|
+
// ~~~~~~~~~~~~~~~~
|
|
810
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
344
811
|
```
|
|
812
|
+
*/
|
|
813
|
+
type ApplyDefaultOptions<
|
|
814
|
+
Options extends object,
|
|
815
|
+
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
816
|
+
SpecifiedOptions extends Options,
|
|
817
|
+
> =
|
|
818
|
+
If<IsAny<SpecifiedOptions>, Defaults,
|
|
819
|
+
If<IsNever<SpecifiedOptions>, Defaults,
|
|
820
|
+
Simplify<Merge<Defaults, {
|
|
821
|
+
[Key in keyof SpecifiedOptions
|
|
822
|
+
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
|
|
823
|
+
]: SpecifiedOptions[Key]
|
|
824
|
+
}> & Required<Options>>>>;
|
|
345
825
|
|
|
346
|
-
|
|
826
|
+
/**
|
|
827
|
+
Filter out keys from an object.
|
|
828
|
+
|
|
829
|
+
Returns `never` if `Exclude` is strictly equal to `Key`.
|
|
830
|
+
Returns `never` if `Key` extends `Exclude`.
|
|
831
|
+
Returns `Key` otherwise.
|
|
832
|
+
|
|
833
|
+
@example
|
|
834
|
+
```
|
|
835
|
+
type Filtered = Filter<'foo', 'foo'>;
|
|
836
|
+
//=> never
|
|
837
|
+
```
|
|
838
|
+
|
|
839
|
+
@example
|
|
840
|
+
```
|
|
841
|
+
type Filtered = Filter<'bar', string>;
|
|
842
|
+
//=> never
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
@example
|
|
846
|
+
```
|
|
847
|
+
type Filtered = Filter<'bar', 'foo'>;
|
|
848
|
+
//=> 'bar'
|
|
849
|
+
```
|
|
850
|
+
|
|
851
|
+
@see {Except}
|
|
347
852
|
*/
|
|
348
|
-
type
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
853
|
+
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
|
|
854
|
+
|
|
855
|
+
type ExceptOptions = {
|
|
856
|
+
/**
|
|
857
|
+
Disallow assigning non-specified properties.
|
|
858
|
+
|
|
859
|
+
Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
|
|
860
|
+
|
|
861
|
+
@default false
|
|
862
|
+
*/
|
|
863
|
+
requireExactProps?: boolean;
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
type DefaultExceptOptions = {
|
|
867
|
+
requireExactProps: false;
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
Create a type from an object type without certain keys.
|
|
872
|
+
|
|
873
|
+
We recommend setting the `requireExactProps` option to `true`.
|
|
874
|
+
|
|
875
|
+
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
|
|
876
|
+
|
|
877
|
+
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
|
|
878
|
+
|
|
879
|
+
@example
|
|
880
|
+
```
|
|
881
|
+
import type {Except} from 'type-fest';
|
|
882
|
+
|
|
883
|
+
type Foo = {
|
|
884
|
+
a: number;
|
|
885
|
+
b: string;
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
type FooWithoutA = Except<Foo, 'a'>;
|
|
889
|
+
//=> {b: string}
|
|
890
|
+
|
|
891
|
+
// @ts-expect-error
|
|
892
|
+
const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
|
|
893
|
+
// errors: 'a' does not exist in type '{ b: string; }'
|
|
894
|
+
|
|
895
|
+
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
|
|
896
|
+
//=> {a: number} & Partial<Record<'b', never>>
|
|
897
|
+
|
|
898
|
+
// @ts-expect-error
|
|
899
|
+
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
|
|
900
|
+
// errors at 'b': Type 'string' is not assignable to type 'undefined'.
|
|
901
|
+
|
|
902
|
+
// The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
|
|
903
|
+
|
|
904
|
+
// Consider the following example:
|
|
905
|
+
|
|
906
|
+
type UserData = {
|
|
907
|
+
[metadata: string]: string;
|
|
908
|
+
email: string;
|
|
909
|
+
name: string;
|
|
910
|
+
role: 'admin' | 'user';
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
// `Omit` clearly doesn't behave as expected in this case:
|
|
914
|
+
type PostPayload = Omit<UserData, 'email'>;
|
|
915
|
+
//=> {[x: string]: string; [x: number]: string}
|
|
916
|
+
|
|
917
|
+
// In situations like this, `Except` works better.
|
|
918
|
+
// It simply removes the `email` key while preserving all the other keys.
|
|
919
|
+
type PostPayloadFixed = Except<UserData, 'email'>;
|
|
920
|
+
//=> {[x: string]: string; name: string; role: 'admin' | 'user'}
|
|
921
|
+
```
|
|
922
|
+
|
|
923
|
+
@category Object
|
|
924
|
+
*/
|
|
925
|
+
type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
|
|
926
|
+
_Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
|
|
927
|
+
|
|
928
|
+
type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
|
|
929
|
+
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
|
|
930
|
+
} & (Options['requireExactProps'] extends true
|
|
931
|
+
? Partial<Record<KeysType, never>>
|
|
932
|
+
: {});
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
Create a type that makes the given keys optional, while keeping the remaining keys as is.
|
|
936
|
+
|
|
937
|
+
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.
|
|
938
|
+
|
|
939
|
+
@example
|
|
940
|
+
```
|
|
941
|
+
import type {SetOptional} from 'type-fest';
|
|
942
|
+
|
|
943
|
+
type Foo = {
|
|
944
|
+
a: number;
|
|
945
|
+
b?: string;
|
|
946
|
+
c: boolean;
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
|
|
950
|
+
//=> {a: number; b?: string; c?: boolean}
|
|
951
|
+
```
|
|
952
|
+
|
|
953
|
+
@category Object
|
|
954
|
+
*/
|
|
955
|
+
type SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
956
|
+
(BaseType extends (...arguments_: never) => any
|
|
957
|
+
? (...arguments_: Parameters<BaseType>) => ReturnType<BaseType>
|
|
958
|
+
: unknown)
|
|
959
|
+
& _SetOptional<BaseType, Keys>;
|
|
960
|
+
|
|
961
|
+
type _SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
962
|
+
BaseType extends unknown // To distribute `BaseType` when it's a union type.
|
|
963
|
+
? Simplify<
|
|
964
|
+
// Pick just the keys that are readonly from the base type.
|
|
965
|
+
Except<BaseType, Keys>
|
|
966
|
+
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
967
|
+
& Partial<HomomorphicPick<BaseType, Keys>>
|
|
968
|
+
>
|
|
362
969
|
: never;
|
|
363
970
|
|
|
364
971
|
interface ReactRenderer extends WebRenderer {
|
|
@@ -500,7 +1107,7 @@ declare function composeStories<TModule extends Store_CSFExports<ReactRenderer,
|
|
|
500
1107
|
|
|
501
1108
|
/** Extracts and unions all args types from an array of decorators. */
|
|
502
1109
|
type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
|
|
503
|
-
type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<
|
|
1110
|
+
type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<OmitIndexSignature<DecoratorsArgs<ReactTypes & T, Decorators>>>>;
|
|
504
1111
|
type InferReactTypes<T, TArgs, Decorators> = ReactTypes & T & {
|
|
505
1112
|
args: Simplify<InferArgs<TArgs, T, Decorators>>;
|
|
506
1113
|
};
|