@storybook/angular 10.5.0-alpha.2 → 10.5.0-alpha.4
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/_node-chunks/{angular-cli-webpack-L5YQQZ22.js → angular-cli-webpack-4WYLBTXL.js} +6 -6
- package/dist/_node-chunks/{chunk-F6PJUYAY.js → chunk-EI76I6HI.js} +6 -6
- package/dist/_node-chunks/{chunk-F4DSBEUH.js → chunk-HKPHE37C.js} +7 -7
- package/dist/builders/build-storybook/index.js +8 -8
- package/dist/builders/start-storybook/index.js +8 -8
- package/dist/index.d.ts +773 -166
- package/dist/node/index.js +6 -6
- package/dist/preset.js +6 -6
- package/dist/server/framework-preset-angular-cli.js +8 -8
- package/dist/server/framework-preset-angular-ivy.js +6 -6
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WebRenderer, Parameters as Parameters$
|
|
1
|
+
import { WebRenderer, Parameters as Parameters$2, Args, ComponentAnnotations, AnnotatedStoryFn, StoryAnnotations, StrictArgs, DecoratorFunction, LoaderFunction, StoryContext as StoryContext$1, ProjectAnnotations, NamedOrDefaultProjectAnnotations, NormalizedProjectAnnotations, Renderer, ArgsStoryFn, StorybookConfig as StorybookConfig$2, Options, CompatibleString, TypescriptOptions as TypescriptOptions$1 } from 'storybook/internal/types';
|
|
2
2
|
export { ArgTypes, Args, Parameters, StrictArgs } from 'storybook/internal/types';
|
|
3
3
|
import * as AngularCore from '@angular/core';
|
|
4
4
|
import { Provider, ApplicationConfig, Type } from '@angular/core';
|
|
@@ -38,7 +38,7 @@ interface AngularRenderer extends WebRenderer {
|
|
|
38
38
|
component: any;
|
|
39
39
|
storyResult: StoryFnAngularReturnType;
|
|
40
40
|
}
|
|
41
|
-
type Parameters = Parameters$
|
|
41
|
+
type Parameters$1 = Parameters$2 & {
|
|
42
42
|
bootstrapModuleOptions?: unknown;
|
|
43
43
|
};
|
|
44
44
|
|
|
@@ -128,99 +128,390 @@ type TransformEventType<T> = {
|
|
|
128
128
|
*/
|
|
129
129
|
declare function setProjectAnnotations(projectAnnotations: NamedOrDefaultProjectAnnotations<any> | NamedOrDefaultProjectAnnotations<any>[]): NormalizedProjectAnnotations<AngularRenderer>;
|
|
130
130
|
|
|
131
|
-
declare global {
|
|
132
|
-
interface SymbolConstructor {
|
|
133
|
-
readonly observable: symbol;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
131
|
/**
|
|
138
|
-
|
|
132
|
+
Convert a union type to an intersection type.
|
|
139
133
|
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
135
|
+
|
|
136
|
+
@example
|
|
137
|
+
```
|
|
138
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
139
|
+
|
|
140
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
141
|
+
|
|
142
|
+
type Intersection = UnionToIntersection<Union>;
|
|
143
|
+
//=> {the(): void} & {great(arg: string): void} & {escape: boolean}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
@category Type
|
|
142
147
|
*/
|
|
143
|
-
type
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
+
type UnionToIntersection<Union> = (
|
|
149
|
+
// `extends unknown` is always going to be the case and is used to convert the
|
|
150
|
+
// `Union` into a [distributive conditional
|
|
151
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
152
|
+
Union extends unknown
|
|
153
|
+
// The union type is used as the only argument to a function since the union
|
|
154
|
+
// of function arguments is an intersection.
|
|
155
|
+
? (distributedUnion: Union) => void
|
|
156
|
+
// This won't happen.
|
|
157
|
+
: never
|
|
158
|
+
// Infer the `Intersection` type since TypeScript represents the positional
|
|
159
|
+
// arguments of unions of functions as an intersection of the union.
|
|
160
|
+
) extends ((mergedIntersection: infer Intersection) => void)
|
|
161
|
+
// The `& Union` is to ensure result of `UnionToIntersection<A | B>` is always assignable to `A | B`
|
|
162
|
+
? Intersection & Union
|
|
163
|
+
: never;
|
|
148
164
|
|
|
149
165
|
/**
|
|
150
|
-
|
|
166
|
+
Create a union of all keys from a given type, even those exclusive to specific union members.
|
|
151
167
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
168
|
+
Unlike the native `keyof` keyword, which returns keys present in **all** union members, this type returns keys from **any** member.
|
|
169
|
+
|
|
170
|
+
@link https://stackoverflow.com/a/49402091
|
|
155
171
|
|
|
156
172
|
@example
|
|
157
173
|
```
|
|
158
|
-
type
|
|
159
|
-
|
|
174
|
+
import type {KeysOfUnion} from 'type-fest';
|
|
175
|
+
|
|
176
|
+
type A = {
|
|
177
|
+
common: string;
|
|
178
|
+
a: number;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
type B = {
|
|
182
|
+
common: string;
|
|
183
|
+
b: string;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
type C = {
|
|
187
|
+
common: string;
|
|
188
|
+
c: boolean;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
type Union = A | B | C;
|
|
192
|
+
|
|
193
|
+
type CommonKeys = keyof Union;
|
|
194
|
+
//=> 'common'
|
|
195
|
+
|
|
196
|
+
type AllKeys = KeysOfUnion<Union>;
|
|
197
|
+
//=> 'common' | 'a' | 'b' | 'c'
|
|
160
198
|
```
|
|
161
199
|
|
|
200
|
+
@category Object
|
|
201
|
+
*/
|
|
202
|
+
type KeysOfUnion<ObjectType> =
|
|
203
|
+
// Hack to fix https://github.com/sindresorhus/type-fest/issues/1008
|
|
204
|
+
keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
Returns a boolean for whether the given type is `any`.
|
|
208
|
+
|
|
209
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
210
|
+
|
|
211
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
212
|
+
|
|
162
213
|
@example
|
|
163
214
|
```
|
|
164
|
-
type
|
|
165
|
-
|
|
215
|
+
import type {IsAny} from 'type-fest';
|
|
216
|
+
|
|
217
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
218
|
+
const anyObject: any = {a: 1, b: 2};
|
|
219
|
+
|
|
220
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
|
|
221
|
+
return object[key];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const typedA = get(typedObject, 'a');
|
|
225
|
+
//=> 1
|
|
226
|
+
|
|
227
|
+
const anyA = get(anyObject, 'a');
|
|
228
|
+
//=> any
|
|
166
229
|
```
|
|
167
230
|
|
|
231
|
+
@category Type Guard
|
|
232
|
+
@category Utilities
|
|
233
|
+
*/
|
|
234
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
Returns a boolean for whether the given key is an optional key of type.
|
|
238
|
+
|
|
239
|
+
This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
|
|
240
|
+
|
|
168
241
|
@example
|
|
169
242
|
```
|
|
170
|
-
type
|
|
171
|
-
|
|
243
|
+
import type {IsOptionalKeyOf} from 'type-fest';
|
|
244
|
+
|
|
245
|
+
type User = {
|
|
246
|
+
name: string;
|
|
247
|
+
surname: string;
|
|
248
|
+
|
|
249
|
+
luckyNumber?: number;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
type Admin = {
|
|
253
|
+
name: string;
|
|
254
|
+
surname?: string;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
258
|
+
//=> true
|
|
259
|
+
|
|
260
|
+
type T2 = IsOptionalKeyOf<User, 'name'>;
|
|
261
|
+
//=> false
|
|
262
|
+
|
|
263
|
+
type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
|
|
264
|
+
//=> boolean
|
|
265
|
+
|
|
266
|
+
type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
|
|
267
|
+
//=> false
|
|
268
|
+
|
|
269
|
+
type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
|
|
270
|
+
//=> boolean
|
|
172
271
|
```
|
|
173
272
|
|
|
174
|
-
@
|
|
273
|
+
@category Type Guard
|
|
274
|
+
@category Utilities
|
|
175
275
|
*/
|
|
176
|
-
type
|
|
276
|
+
type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
|
|
277
|
+
IsAny<Type | Key> extends true ? never
|
|
278
|
+
: Key extends keyof Type
|
|
279
|
+
? Type extends Record<Key, Type[Key]>
|
|
280
|
+
? false
|
|
281
|
+
: true
|
|
282
|
+
: false;
|
|
177
283
|
|
|
178
284
|
/**
|
|
179
|
-
|
|
285
|
+
Extract all optional keys from the given type.
|
|
180
286
|
|
|
181
|
-
This
|
|
182
|
-
|
|
183
|
-
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)).
|
|
287
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
184
288
|
|
|
185
289
|
@example
|
|
186
290
|
```
|
|
187
|
-
import type {Except} from 'type-fest';
|
|
291
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
188
292
|
|
|
189
|
-
type
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
293
|
+
type User = {
|
|
294
|
+
name: string;
|
|
295
|
+
surname: string;
|
|
296
|
+
|
|
297
|
+
luckyNumber?: number;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
301
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
302
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
193
303
|
};
|
|
194
304
|
|
|
195
|
-
|
|
196
|
-
|
|
305
|
+
const update1: UpdateOperation<User> = {
|
|
306
|
+
name: 'Alice',
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const update2: UpdateOperation<User> = {
|
|
310
|
+
name: 'Bob',
|
|
311
|
+
luckyNumber: REMOVE_FIELD,
|
|
312
|
+
};
|
|
197
313
|
```
|
|
198
314
|
|
|
199
|
-
@category
|
|
315
|
+
@category Utilities
|
|
200
316
|
*/
|
|
201
|
-
type
|
|
202
|
-
|
|
317
|
+
type OptionalKeysOf<Type extends object> =
|
|
318
|
+
Type extends unknown // For distributing `Type`
|
|
319
|
+
? (keyof {[Key in keyof Type as
|
|
320
|
+
IsOptionalKeyOf<Type, Key> extends false
|
|
321
|
+
? never
|
|
322
|
+
: Key
|
|
323
|
+
]: never
|
|
324
|
+
}) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
|
|
325
|
+
: never; // Should never happen
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
Extract all required keys from the given type.
|
|
329
|
+
|
|
330
|
+
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...
|
|
331
|
+
|
|
332
|
+
@example
|
|
333
|
+
```
|
|
334
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
335
|
+
|
|
336
|
+
declare function createValidation<
|
|
337
|
+
Entity extends object,
|
|
338
|
+
Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
|
|
339
|
+
>(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
|
|
340
|
+
|
|
341
|
+
type User = {
|
|
342
|
+
name: string;
|
|
343
|
+
surname: string;
|
|
344
|
+
luckyNumber?: number;
|
|
203
345
|
};
|
|
204
346
|
|
|
347
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
348
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
349
|
+
|
|
350
|
+
// @ts-expect-error
|
|
351
|
+
const validator3 = createValidation<User>('luckyNumber', value => value > 0);
|
|
352
|
+
// Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
@category Utilities
|
|
356
|
+
*/
|
|
357
|
+
type RequiredKeysOf<Type extends object> =
|
|
358
|
+
Type extends unknown // For distributing `Type`
|
|
359
|
+
? Exclude<keyof Type, OptionalKeysOf<Type>>
|
|
360
|
+
: never; // Should never happen
|
|
361
|
+
|
|
205
362
|
/**
|
|
206
|
-
|
|
363
|
+
Returns a boolean for whether the given type is `never`.
|
|
364
|
+
|
|
365
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
366
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
367
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
368
|
+
|
|
369
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
370
|
+
|
|
371
|
+
@example
|
|
372
|
+
```
|
|
373
|
+
import type {IsNever, And} from 'type-fest';
|
|
374
|
+
|
|
375
|
+
type A = IsNever<never>;
|
|
376
|
+
//=> true
|
|
377
|
+
|
|
378
|
+
type B = IsNever<any>;
|
|
379
|
+
//=> false
|
|
380
|
+
|
|
381
|
+
type C = IsNever<unknown>;
|
|
382
|
+
//=> false
|
|
383
|
+
|
|
384
|
+
type D = IsNever<never[]>;
|
|
385
|
+
//=> false
|
|
386
|
+
|
|
387
|
+
type E = IsNever<object>;
|
|
388
|
+
//=> false
|
|
389
|
+
|
|
390
|
+
type F = IsNever<string>;
|
|
391
|
+
//=> false
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
@example
|
|
395
|
+
```
|
|
396
|
+
import type {IsNever} from 'type-fest';
|
|
397
|
+
|
|
398
|
+
type IsTrue<T> = T extends true ? true : false;
|
|
399
|
+
|
|
400
|
+
// When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
|
|
401
|
+
type A = IsTrue<never>;
|
|
402
|
+
//=> never
|
|
403
|
+
|
|
404
|
+
// If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
|
|
405
|
+
type IsTrueFixed<T> =
|
|
406
|
+
IsNever<T> extends true ? false : T extends true ? true : false;
|
|
407
|
+
|
|
408
|
+
type B = IsTrueFixed<never>;
|
|
409
|
+
//=> false
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
@category Type Guard
|
|
413
|
+
@category Utilities
|
|
207
414
|
*/
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
Do the simplification recursively.
|
|
415
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
211
416
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
deep?: boolean;
|
|
215
|
-
}
|
|
417
|
+
/**
|
|
418
|
+
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
216
419
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
420
|
+
Use-cases:
|
|
421
|
+
- 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'>`.
|
|
422
|
+
|
|
423
|
+
Note:
|
|
424
|
+
- 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'`.
|
|
425
|
+
- Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
|
|
426
|
+
|
|
427
|
+
@example
|
|
428
|
+
```
|
|
429
|
+
import type {If} from 'type-fest';
|
|
430
|
+
|
|
431
|
+
type A = If<true, 'yes', 'no'>;
|
|
432
|
+
//=> 'yes'
|
|
433
|
+
|
|
434
|
+
type B = If<false, 'yes', 'no'>;
|
|
435
|
+
//=> 'no'
|
|
436
|
+
|
|
437
|
+
type C = If<boolean, 'yes', 'no'>;
|
|
438
|
+
//=> 'yes' | 'no'
|
|
439
|
+
|
|
440
|
+
type D = If<any, 'yes', 'no'>;
|
|
441
|
+
//=> 'yes' | 'no'
|
|
442
|
+
|
|
443
|
+
type E = If<never, 'yes', 'no'>;
|
|
444
|
+
//=> 'no'
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
@example
|
|
448
|
+
```
|
|
449
|
+
import type {If, IsAny, IsNever} from 'type-fest';
|
|
450
|
+
|
|
451
|
+
type A = If<IsAny<unknown>, 'is any', 'not any'>;
|
|
452
|
+
//=> 'not any'
|
|
453
|
+
|
|
454
|
+
type B = If<IsNever<never>, 'is never', 'not never'>;
|
|
455
|
+
//=> 'is never'
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
@example
|
|
459
|
+
```
|
|
460
|
+
import type {If, IsEqual} from 'type-fest';
|
|
461
|
+
|
|
462
|
+
type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
|
|
463
|
+
|
|
464
|
+
type A = IfEqual<string, string, 'equal', 'not equal'>;
|
|
465
|
+
//=> 'equal'
|
|
466
|
+
|
|
467
|
+
type B = IfEqual<string, number, 'equal', 'not equal'>;
|
|
468
|
+
//=> 'not equal'
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
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:
|
|
472
|
+
|
|
473
|
+
@example
|
|
474
|
+
```
|
|
475
|
+
import type {If, IsEqual, StringRepeat} from 'type-fest';
|
|
476
|
+
|
|
477
|
+
type HundredZeroes = StringRepeat<'0', 100>;
|
|
478
|
+
|
|
479
|
+
// The following implementation is not tail recursive
|
|
480
|
+
type Includes<S extends string, Char extends string> =
|
|
481
|
+
S extends `${infer First}${infer Rest}`
|
|
482
|
+
? If<IsEqual<First, Char>,
|
|
483
|
+
'found',
|
|
484
|
+
Includes<Rest, Char>>
|
|
485
|
+
: 'not found';
|
|
486
|
+
|
|
487
|
+
// Hence, instantiations with long strings will fail
|
|
488
|
+
// @ts-expect-error
|
|
489
|
+
type Fails = Includes<HundredZeroes, '1'>;
|
|
490
|
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
491
|
+
// Error: Type instantiation is excessively deep and possibly infinite.
|
|
492
|
+
|
|
493
|
+
// However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive
|
|
494
|
+
type IncludesWithoutIf<S extends string, Char extends string> =
|
|
495
|
+
S extends `${infer First}${infer Rest}`
|
|
496
|
+
? IsEqual<First, Char> extends true
|
|
497
|
+
? 'found'
|
|
498
|
+
: IncludesWithoutIf<Rest, Char>
|
|
499
|
+
: 'not found';
|
|
500
|
+
|
|
501
|
+
// Now, instantiations with long strings will work
|
|
502
|
+
type Works = IncludesWithoutIf<HundredZeroes, '1'>;
|
|
503
|
+
//=> 'not found'
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
@category Type Guard
|
|
507
|
+
@category Utilities
|
|
508
|
+
*/
|
|
509
|
+
type If<Type extends boolean, IfBranch, ElseBranch> =
|
|
510
|
+
IsNever<Type> extends true
|
|
511
|
+
? ElseBranch
|
|
512
|
+
: Type extends true
|
|
513
|
+
? IfBranch
|
|
514
|
+
: ElseBranch;
|
|
224
515
|
|
|
225
516
|
/**
|
|
226
517
|
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.
|
|
@@ -267,27 +558,65 @@ const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
|
267
558
|
const someType: SomeType = literal;
|
|
268
559
|
const someInterface: SomeInterface = literal;
|
|
269
560
|
|
|
270
|
-
function fn(object: Record<string, unknown>): void
|
|
561
|
+
declare function fn(object: Record<string, unknown>): void;
|
|
271
562
|
|
|
272
563
|
fn(literal); // Good: literal object type is sealed
|
|
273
564
|
fn(someType); // Good: type is sealed
|
|
565
|
+
// @ts-expect-error
|
|
274
566
|
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
275
567
|
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
276
568
|
```
|
|
277
569
|
|
|
278
570
|
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
279
|
-
|
|
571
|
+
@see {@link SimplifyDeep}
|
|
280
572
|
@category Object
|
|
281
573
|
*/
|
|
282
|
-
type Simplify<
|
|
283
|
-
AnyType,
|
|
284
|
-
Options extends SimplifyOptions = {},
|
|
285
|
-
> = Flatten<AnyType> extends AnyType
|
|
286
|
-
? Flatten<AnyType, Options>
|
|
287
|
-
: AnyType;
|
|
574
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
288
575
|
|
|
289
576
|
/**
|
|
290
|
-
|
|
577
|
+
Returns a boolean for whether the two given types are equal.
|
|
578
|
+
|
|
579
|
+
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
580
|
+
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
581
|
+
|
|
582
|
+
Use-cases:
|
|
583
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
584
|
+
|
|
585
|
+
@example
|
|
586
|
+
```
|
|
587
|
+
import type {IsEqual} from 'type-fest';
|
|
588
|
+
|
|
589
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
590
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
591
|
+
type Includes<Value extends readonly any[], Item> =
|
|
592
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
593
|
+
? IsEqual<Value[0], Item> extends true
|
|
594
|
+
? true
|
|
595
|
+
: Includes<rest, Item>
|
|
596
|
+
: false;
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
@category Type Guard
|
|
600
|
+
@category Utilities
|
|
601
|
+
*/
|
|
602
|
+
type IsEqual<A, B> =
|
|
603
|
+
[A] extends [B]
|
|
604
|
+
? [B] extends [A]
|
|
605
|
+
? _IsEqual<A, B>
|
|
606
|
+
: false
|
|
607
|
+
: false;
|
|
608
|
+
|
|
609
|
+
// This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
|
|
610
|
+
type _IsEqual<A, B> =
|
|
611
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
612
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
613
|
+
? true
|
|
614
|
+
: false;
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
618
|
+
|
|
619
|
+
This is the counterpart of `PickIndexSignature`.
|
|
291
620
|
|
|
292
621
|
Use-cases:
|
|
293
622
|
- Remove overly permissive signatures from third-party types.
|
|
@@ -301,8 +630,9 @@ It relies on the fact that an empty object (`{}`) is assignable to an object wit
|
|
|
301
630
|
```
|
|
302
631
|
const indexed: Record<string, unknown> = {}; // Allowed
|
|
303
632
|
|
|
633
|
+
// @ts-expect-error
|
|
304
634
|
const keyed: Record<'foo', unknown> = {}; // Error
|
|
305
|
-
//
|
|
635
|
+
// TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
306
636
|
```
|
|
307
637
|
|
|
308
638
|
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:
|
|
@@ -311,178 +641,455 @@ Instead of causing a type error like the above, you can also use a [conditional
|
|
|
311
641
|
type Indexed = {} extends Record<string, unknown>
|
|
312
642
|
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
313
643
|
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
314
|
-
|
|
644
|
+
|
|
645
|
+
type IndexedResult = Indexed;
|
|
646
|
+
//=> '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
315
647
|
|
|
316
648
|
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
317
|
-
?
|
|
318
|
-
:
|
|
319
|
-
|
|
649
|
+
? '✅ `{}` is assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
650
|
+
: '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`';
|
|
651
|
+
|
|
652
|
+
type KeyedResult = Keyed;
|
|
653
|
+
//=> '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
320
654
|
```
|
|
321
655
|
|
|
322
656
|
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`...
|
|
323
657
|
|
|
324
658
|
```
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
659
|
+
type OmitIndexSignature<ObjectType> = {
|
|
328
660
|
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
329
|
-
]: ObjectType[KeyType]; // ...to its original value, i.e. `
|
|
661
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
330
662
|
};
|
|
331
663
|
```
|
|
332
664
|
|
|
333
665
|
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
334
666
|
|
|
335
667
|
```
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
668
|
+
type OmitIndexSignature<ObjectType> = {
|
|
339
669
|
[KeyType in keyof ObjectType
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
670
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
671
|
+
as {} extends Record<KeyType, unknown>
|
|
672
|
+
? never // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
673
|
+
: KeyType // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
344
674
|
]: ObjectType[KeyType];
|
|
345
675
|
};
|
|
346
676
|
```
|
|
347
677
|
|
|
348
678
|
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.
|
|
349
679
|
|
|
350
|
-
```
|
|
351
|
-
import type {RemoveIndexSignature} from 'type-fest';
|
|
352
|
-
|
|
353
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
354
|
-
[KeyType in keyof ObjectType
|
|
355
|
-
as {} extends Record<KeyType, unknown>
|
|
356
|
-
? never // => Remove this `KeyType`.
|
|
357
|
-
: KeyType // => Keep this `KeyType` as it is.
|
|
358
|
-
]: ObjectType[KeyType];
|
|
359
|
-
};
|
|
360
|
-
```
|
|
361
|
-
|
|
362
680
|
@example
|
|
363
681
|
```
|
|
364
|
-
import type {
|
|
682
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
365
683
|
|
|
366
|
-
|
|
684
|
+
type Example = {
|
|
367
685
|
// These index signatures will be removed.
|
|
368
|
-
[x: string]: any
|
|
369
|
-
[x: number]: any
|
|
370
|
-
[x: symbol]: any
|
|
371
|
-
[x: `head-${string}`]: string
|
|
372
|
-
[x: `${string}-tail`]: string
|
|
373
|
-
[x: `head-${string}-tail`]: string
|
|
374
|
-
[x: `${bigint}`]: string
|
|
375
|
-
[x: `embedded-${number}`]: string
|
|
686
|
+
[x: string]: any;
|
|
687
|
+
[x: number]: any;
|
|
688
|
+
[x: symbol]: any;
|
|
689
|
+
[x: `head-${string}`]: string;
|
|
690
|
+
[x: `${string}-tail`]: string;
|
|
691
|
+
[x: `head-${string}-tail`]: string;
|
|
692
|
+
[x: `${bigint}`]: string;
|
|
693
|
+
[x: `embedded-${number}`]: string;
|
|
376
694
|
|
|
377
695
|
// These explicitly defined keys will remain.
|
|
378
696
|
foo: 'bar';
|
|
379
697
|
qux?: 'baz';
|
|
380
|
-
}
|
|
698
|
+
};
|
|
381
699
|
|
|
382
|
-
type ExampleWithoutIndexSignatures =
|
|
383
|
-
|
|
700
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
701
|
+
//=> {foo: 'bar'; qux?: 'baz'}
|
|
384
702
|
```
|
|
385
703
|
|
|
704
|
+
@see {@link PickIndexSignature}
|
|
386
705
|
@category Object
|
|
387
706
|
*/
|
|
388
|
-
type
|
|
707
|
+
type OmitIndexSignature<ObjectType> = {
|
|
389
708
|
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
390
709
|
? never
|
|
391
710
|
: KeyType]: ObjectType[KeyType];
|
|
392
711
|
};
|
|
393
712
|
|
|
394
713
|
/**
|
|
395
|
-
|
|
714
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
396
715
|
|
|
397
|
-
|
|
716
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
398
717
|
|
|
399
718
|
@example
|
|
400
719
|
```
|
|
401
|
-
import type {
|
|
720
|
+
import type {PickIndexSignature} from 'type-fest';
|
|
721
|
+
|
|
722
|
+
declare const symbolKey: unique symbol;
|
|
723
|
+
|
|
724
|
+
type Example = {
|
|
725
|
+
// These index signatures will remain.
|
|
726
|
+
[x: string]: unknown;
|
|
727
|
+
[x: number]: unknown;
|
|
728
|
+
[x: symbol]: unknown;
|
|
729
|
+
[x: `head-${string}`]: string;
|
|
730
|
+
[x: `${string}-tail`]: string;
|
|
731
|
+
[x: `head-${string}-tail`]: string;
|
|
732
|
+
[x: `${bigint}`]: string;
|
|
733
|
+
[x: `embedded-${number}`]: string;
|
|
734
|
+
|
|
735
|
+
// These explicitly defined keys will be removed.
|
|
736
|
+
['kebab-case-key']: string;
|
|
737
|
+
[symbolKey]: string;
|
|
738
|
+
foo: 'bar';
|
|
739
|
+
qux?: 'baz';
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
743
|
+
// {
|
|
744
|
+
// [x: string]: unknown;
|
|
745
|
+
// [x: number]: unknown;
|
|
746
|
+
// [x: symbol]: unknown;
|
|
747
|
+
// [x: `head-${string}`]: string;
|
|
748
|
+
// [x: `${string}-tail`]: string;
|
|
749
|
+
// [x: `head-${string}-tail`]: string;
|
|
750
|
+
// [x: `${bigint}`]: string;
|
|
751
|
+
// [x: `embedded-${number}`]: string;
|
|
752
|
+
// }
|
|
753
|
+
```
|
|
754
|
+
|
|
755
|
+
@see {@link OmitIndexSignature}
|
|
756
|
+
@category Object
|
|
757
|
+
*/
|
|
758
|
+
type PickIndexSignature<ObjectType> = {
|
|
759
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
760
|
+
? KeyType
|
|
761
|
+
: never]: ObjectType[KeyType];
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
// Merges two objects without worrying about index signatures.
|
|
765
|
+
type SimpleMerge<Destination, Source> = Simplify<{
|
|
766
|
+
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
767
|
+
} & Source>;
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
771
|
+
|
|
772
|
+
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`.
|
|
773
|
+
|
|
774
|
+
@example
|
|
775
|
+
```
|
|
776
|
+
import type {Merge} from 'type-fest';
|
|
402
777
|
|
|
403
778
|
type Foo = {
|
|
404
|
-
a:
|
|
405
|
-
b
|
|
779
|
+
a: string;
|
|
780
|
+
b: number;
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
type Bar = {
|
|
784
|
+
a: number; // Conflicts with Foo['a']
|
|
406
785
|
c: boolean;
|
|
407
|
-
}
|
|
786
|
+
};
|
|
408
787
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
//
|
|
788
|
+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
|
|
789
|
+
type WithIntersection = (Foo & Bar)['a'];
|
|
790
|
+
//=> never
|
|
791
|
+
|
|
792
|
+
// With `Merge`, `a` is cleanly overridden to `number`.
|
|
793
|
+
type WithMerge = Merge<Foo, Bar>['a'];
|
|
794
|
+
//=> number
|
|
795
|
+
```
|
|
796
|
+
|
|
797
|
+
@example
|
|
798
|
+
```
|
|
799
|
+
import type {Merge} from 'type-fest';
|
|
800
|
+
|
|
801
|
+
type Foo = {
|
|
802
|
+
[x: string]: unknown;
|
|
803
|
+
[x: number]: unknown;
|
|
804
|
+
foo: string;
|
|
805
|
+
bar: symbol;
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
type Bar = {
|
|
809
|
+
[x: number]: number;
|
|
810
|
+
[x: symbol]: unknown;
|
|
811
|
+
bar: Date;
|
|
812
|
+
baz: boolean;
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
export type FooBar = Merge<Foo, Bar>;
|
|
816
|
+
//=> {
|
|
817
|
+
// [x: string]: unknown;
|
|
818
|
+
// [x: number]: number;
|
|
819
|
+
// [x: symbol]: unknown;
|
|
820
|
+
// foo: string;
|
|
821
|
+
// bar: Date;
|
|
822
|
+
// baz: boolean;
|
|
414
823
|
// }
|
|
415
824
|
```
|
|
416
825
|
|
|
826
|
+
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.
|
|
827
|
+
|
|
828
|
+
@see {@link ObjectMerge}
|
|
417
829
|
@category Object
|
|
418
830
|
*/
|
|
419
|
-
type
|
|
831
|
+
type Merge<Destination, Source> =
|
|
832
|
+
Destination extends unknown // For distributing `Destination`
|
|
833
|
+
? Source extends unknown // For distributing `Source`
|
|
834
|
+
? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
|
|
835
|
+
: never // Should never happen
|
|
836
|
+
: never; // Should never happen
|
|
837
|
+
|
|
838
|
+
type _Merge<Destination, Source> =
|
|
420
839
|
Simplify<
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
424
|
-
Partial<Pick<BaseType, Keys>>
|
|
840
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
841
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
425
842
|
>;
|
|
426
843
|
|
|
427
844
|
/**
|
|
428
|
-
|
|
845
|
+
Works similar to the built-in `Pick` utility type, except for the following differences:
|
|
846
|
+
- Distributes over union types and allows picking keys from any member of the union type.
|
|
847
|
+
- Primitives types are returned as-is.
|
|
848
|
+
- Picks all keys if `Keys` is `any`.
|
|
849
|
+
- Doesn't pick `number` from a `string` index signature.
|
|
429
850
|
|
|
430
|
-
|
|
851
|
+
@example
|
|
852
|
+
```
|
|
853
|
+
type ImageUpload = {
|
|
854
|
+
url: string;
|
|
855
|
+
size: number;
|
|
856
|
+
thumbnailUrl: string;
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
type VideoUpload = {
|
|
860
|
+
url: string;
|
|
861
|
+
duration: number;
|
|
862
|
+
encodingFormat: string;
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
// Distributes over union types and allows picking keys from any member of the union type
|
|
866
|
+
type MediaDisplay = HomomorphicPick<ImageUpload | VideoUpload, "url" | "size" | "duration">;
|
|
867
|
+
//=> {url: string; size: number} | {url: string; duration: number}
|
|
868
|
+
|
|
869
|
+
// Primitive types are returned as-is
|
|
870
|
+
type Primitive = HomomorphicPick<string | number, 'toUpperCase' | 'toString'>;
|
|
871
|
+
//=> string | number
|
|
872
|
+
|
|
873
|
+
// Picks all keys if `Keys` is `any`
|
|
874
|
+
type Any = HomomorphicPick<{a: 1; b: 2} | {c: 3}, any>;
|
|
875
|
+
//=> {a: 1; b: 2} | {c: 3}
|
|
876
|
+
|
|
877
|
+
// Doesn't pick `number` from a `string` index signature
|
|
878
|
+
type IndexSignature = HomomorphicPick<{[k: string]: unknown}, number>;
|
|
879
|
+
//=> {}
|
|
880
|
+
*/
|
|
881
|
+
type HomomorphicPick<T, Keys extends KeysOfUnion<T>> = {
|
|
882
|
+
[P in keyof T as Extract<P, Keys>]: T[P]
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
Merges user specified options with default options.
|
|
431
887
|
|
|
432
888
|
@example
|
|
433
889
|
```
|
|
434
|
-
|
|
890
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
891
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
892
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
435
893
|
|
|
436
|
-
type
|
|
894
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
895
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
896
|
+
```
|
|
437
897
|
|
|
438
|
-
|
|
439
|
-
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
898
|
+
@example
|
|
440
899
|
```
|
|
900
|
+
// Complains if default values are not provided for optional options
|
|
441
901
|
|
|
442
|
-
|
|
902
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
903
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
904
|
+
type SpecifiedOptions = {};
|
|
905
|
+
|
|
906
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
907
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
908
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
909
|
+
```
|
|
443
910
|
|
|
444
911
|
@example
|
|
445
912
|
```
|
|
446
|
-
|
|
913
|
+
// Complains if an option's default type does not conform to the expected type
|
|
447
914
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
b1: () => undefined,
|
|
452
|
-
}
|
|
453
|
-
}
|
|
915
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
916
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
917
|
+
type SpecifiedOptions = {};
|
|
454
918
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
}
|
|
919
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
920
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
921
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
922
|
+
```
|
|
461
923
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
924
|
+
@example
|
|
925
|
+
```
|
|
926
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
465
927
|
|
|
466
|
-
type
|
|
467
|
-
|
|
928
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
929
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
930
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
931
|
+
|
|
932
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
933
|
+
// ~~~~~~~~~~~~~~~~
|
|
934
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
468
935
|
```
|
|
936
|
+
*/
|
|
937
|
+
type ApplyDefaultOptions<
|
|
938
|
+
Options extends object,
|
|
939
|
+
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
940
|
+
SpecifiedOptions extends Options,
|
|
941
|
+
> =
|
|
942
|
+
If<IsAny<SpecifiedOptions>, Defaults,
|
|
943
|
+
If<IsNever<SpecifiedOptions>, Defaults,
|
|
944
|
+
Simplify<Merge<Defaults, {
|
|
945
|
+
[Key in keyof SpecifiedOptions
|
|
946
|
+
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
|
|
947
|
+
]: SpecifiedOptions[Key]
|
|
948
|
+
}> & Required<Options>>>>;
|
|
469
949
|
|
|
470
|
-
|
|
950
|
+
/**
|
|
951
|
+
Filter out keys from an object.
|
|
952
|
+
|
|
953
|
+
Returns `never` if `Exclude` is strictly equal to `Key`.
|
|
954
|
+
Returns `never` if `Key` extends `Exclude`.
|
|
955
|
+
Returns `Key` otherwise.
|
|
956
|
+
|
|
957
|
+
@example
|
|
958
|
+
```
|
|
959
|
+
type Filtered = Filter<'foo', 'foo'>;
|
|
960
|
+
//=> never
|
|
961
|
+
```
|
|
962
|
+
|
|
963
|
+
@example
|
|
964
|
+
```
|
|
965
|
+
type Filtered = Filter<'bar', string>;
|
|
966
|
+
//=> never
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
@example
|
|
970
|
+
```
|
|
971
|
+
type Filtered = Filter<'bar', 'foo'>;
|
|
972
|
+
//=> 'bar'
|
|
973
|
+
```
|
|
974
|
+
|
|
975
|
+
@see {Except}
|
|
471
976
|
*/
|
|
472
|
-
type
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
977
|
+
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
|
|
978
|
+
|
|
979
|
+
type ExceptOptions = {
|
|
980
|
+
/**
|
|
981
|
+
Disallow assigning non-specified properties.
|
|
982
|
+
|
|
983
|
+
Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
|
|
984
|
+
|
|
985
|
+
@default false
|
|
986
|
+
*/
|
|
987
|
+
requireExactProps?: boolean;
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
type DefaultExceptOptions = {
|
|
991
|
+
requireExactProps: false;
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
/**
|
|
995
|
+
Create a type from an object type without certain keys.
|
|
996
|
+
|
|
997
|
+
We recommend setting the `requireExactProps` option to `true`.
|
|
998
|
+
|
|
999
|
+
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.
|
|
1000
|
+
|
|
1001
|
+
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)).
|
|
1002
|
+
|
|
1003
|
+
@example
|
|
1004
|
+
```
|
|
1005
|
+
import type {Except} from 'type-fest';
|
|
1006
|
+
|
|
1007
|
+
type Foo = {
|
|
1008
|
+
a: number;
|
|
1009
|
+
b: string;
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
type FooWithoutA = Except<Foo, 'a'>;
|
|
1013
|
+
//=> {b: string}
|
|
1014
|
+
|
|
1015
|
+
// @ts-expect-error
|
|
1016
|
+
const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
|
|
1017
|
+
// errors: 'a' does not exist in type '{ b: string; }'
|
|
1018
|
+
|
|
1019
|
+
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
|
|
1020
|
+
//=> {a: number} & Partial<Record<'b', never>>
|
|
1021
|
+
|
|
1022
|
+
// @ts-expect-error
|
|
1023
|
+
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
|
|
1024
|
+
// errors at 'b': Type 'string' is not assignable to type 'undefined'.
|
|
1025
|
+
|
|
1026
|
+
// The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
|
|
1027
|
+
|
|
1028
|
+
// Consider the following example:
|
|
1029
|
+
|
|
1030
|
+
type UserData = {
|
|
1031
|
+
[metadata: string]: string;
|
|
1032
|
+
email: string;
|
|
1033
|
+
name: string;
|
|
1034
|
+
role: 'admin' | 'user';
|
|
1035
|
+
};
|
|
1036
|
+
|
|
1037
|
+
// `Omit` clearly doesn't behave as expected in this case:
|
|
1038
|
+
type PostPayload = Omit<UserData, 'email'>;
|
|
1039
|
+
//=> {[x: string]: string; [x: number]: string}
|
|
1040
|
+
|
|
1041
|
+
// In situations like this, `Except` works better.
|
|
1042
|
+
// It simply removes the `email` key while preserving all the other keys.
|
|
1043
|
+
type PostPayloadFixed = Except<UserData, 'email'>;
|
|
1044
|
+
//=> {[x: string]: string; name: string; role: 'admin' | 'user'}
|
|
1045
|
+
```
|
|
1046
|
+
|
|
1047
|
+
@category Object
|
|
1048
|
+
*/
|
|
1049
|
+
type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
|
|
1050
|
+
_Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
|
|
1051
|
+
|
|
1052
|
+
type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
|
|
1053
|
+
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
|
|
1054
|
+
} & (Options['requireExactProps'] extends true
|
|
1055
|
+
? Partial<Record<KeysType, never>>
|
|
1056
|
+
: {});
|
|
1057
|
+
|
|
1058
|
+
/**
|
|
1059
|
+
Create a type that makes the given keys optional, while keeping the remaining keys as is.
|
|
1060
|
+
|
|
1061
|
+
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.
|
|
1062
|
+
|
|
1063
|
+
@example
|
|
1064
|
+
```
|
|
1065
|
+
import type {SetOptional} from 'type-fest';
|
|
1066
|
+
|
|
1067
|
+
type Foo = {
|
|
1068
|
+
a: number;
|
|
1069
|
+
b?: string;
|
|
1070
|
+
c: boolean;
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
|
|
1074
|
+
//=> {a: number; b?: string; c?: boolean}
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
@category Object
|
|
1078
|
+
*/
|
|
1079
|
+
type SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
1080
|
+
(BaseType extends (...arguments_: never) => any
|
|
1081
|
+
? (...arguments_: Parameters<BaseType>) => ReturnType<BaseType>
|
|
1082
|
+
: unknown)
|
|
1083
|
+
& _SetOptional<BaseType, Keys>;
|
|
1084
|
+
|
|
1085
|
+
type _SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
1086
|
+
BaseType extends unknown // To distribute `BaseType` when it's a union type.
|
|
1087
|
+
? Simplify<
|
|
1088
|
+
// Pick just the keys that are readonly from the base type.
|
|
1089
|
+
Except<BaseType, Keys>
|
|
1090
|
+
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
1091
|
+
& Partial<HomomorphicPick<BaseType, Keys>>
|
|
1092
|
+
>
|
|
486
1093
|
: never;
|
|
487
1094
|
|
|
488
1095
|
/**
|
|
@@ -507,7 +1114,7 @@ type UnionToIntersection<Union> = (
|
|
|
507
1114
|
declare function __definePreview<Addons extends PreviewAddon<never>[]>(input: {
|
|
508
1115
|
addons: Addons;
|
|
509
1116
|
} & ProjectAnnotations<AngularRenderer & InferTypes<Addons>>): AngularPreview<AngularRenderer & InferTypes<Addons>>;
|
|
510
|
-
type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<
|
|
1117
|
+
type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<OmitIndexSignature<DecoratorsArgs<AngularRenderer & T, Decorators>>>>;
|
|
511
1118
|
type InferComponentArgs<C extends abstract new (...args: any) => any> = Partial<TransformComponentType<InstanceType<C>>>;
|
|
512
1119
|
type InferAngularTypes<T, TArgs, Decorators> = AngularRenderer & T & {
|
|
513
1120
|
args: Simplify<InferArgs<TArgs, T, Decorators>>;
|
|
@@ -758,4 +1365,4 @@ interface AngularOptions {
|
|
|
758
1365
|
enableIvy?: boolean;
|
|
759
1366
|
}
|
|
760
1367
|
|
|
761
|
-
export { type AngularMeta, type AngularOptions, type Parameters as AngularParameters, type AngularPreview, type AngularRenderer, type AngularStory, type Decorator, type FrameworkOptions, type StoryFnAngularReturnType as IStory, type Loader, type Meta, type Preview, type StoryContext, type StoryFn, type StoryObj, type StorybookConfig, type TransformComponentType, __definePreview, applicationConfig, argsToTemplate, componentWrapperDecorator, __definePreview as definePreview, moduleMetadata, setProjectAnnotations };
|
|
1368
|
+
export { type AngularMeta, type AngularOptions, type Parameters$1 as AngularParameters, type AngularPreview, type AngularRenderer, type AngularStory, type Decorator, type FrameworkOptions, type StoryFnAngularReturnType as IStory, type Loader, type Meta, type Preview, type StoryContext, type StoryFn, type StoryObj, type StorybookConfig, type TransformComponentType, __definePreview, applicationConfig, argsToTemplate, componentWrapperDecorator, __definePreview as definePreview, moduleMetadata, setProjectAnnotations };
|