@storybook/angular 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/_browser-chunks/{chunk-6PLRDPVY.js → chunk-2UO6R2HQ.js} +27 -3
- package/dist/_browser-chunks/{chunk-QQWSC6HR.js → chunk-KH22LQ5L.js} +24 -4
- package/dist/_browser-chunks/{chunk-KKFRD2ZH.js → chunk-TTO6V6KM.js} +1 -1
- package/dist/_browser-chunks/{chunk-6IPUGLI5.js → chunk-XHXUBY4P.js} +3 -3
- package/dist/_node-chunks/{angular-cli-webpack-5PCSFBDQ.js → angular-cli-webpack-JX45NT5M.js} +6 -6
- package/dist/_node-chunks/{chunk-ONUOPUYM.js → chunk-72DW2AVB.js} +7 -7
- package/dist/_node-chunks/{chunk-MEOQKDJV.js → chunk-H7ERC6YA.js} +6 -6
- package/dist/builders/build-storybook/index.js +8 -8
- package/dist/builders/start-storybook/index.js +8 -8
- package/dist/client/config.js +2 -2
- package/dist/client/docs/config.js +2 -2
- package/dist/client/index.js +4 -4
- package/dist/index.d.ts +793 -168
- package/dist/index.js +4 -4
- 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
|
|
|
@@ -64,26 +64,44 @@ type Decorator<TArgs = StrictArgs> = DecoratorFunction<AngularRenderer, TArgs>;
|
|
|
64
64
|
type Loader<TArgs = StrictArgs> = LoaderFunction<AngularRenderer, TArgs>;
|
|
65
65
|
type StoryContext<TArgs = StrictArgs> = StoryContext$1<AngularRenderer, TArgs>;
|
|
66
66
|
type Preview = ProjectAnnotations<AngularRenderer>;
|
|
67
|
-
/**
|
|
68
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Transforms InputSignal, ModelSignal, OutputEmitterRef and EventEmitter member
|
|
69
|
+
* types into the values/handlers Storybook args expect.
|
|
70
|
+
*
|
|
71
|
+
* Do NOT reorder: `TransformModelSignalType` must stay innermost. It synthesizes
|
|
72
|
+
* the `${K}Change` output key before the outer transforms run, and because
|
|
73
|
+
* `ModelSignal<T> extends InputSignal<T>` the model value field is then
|
|
74
|
+
* idempotently re-collapsed by `TransformInputSignalType` to the same type.
|
|
75
|
+
*/
|
|
76
|
+
type TransformComponentType<T> = TransformInputSignalType<TransformOutputSignalType<TransformEventType<TransformModelSignalType<T>>>>;
|
|
69
77
|
type AngularInputSignal<T> = AngularCore.InputSignal<T>;
|
|
70
78
|
type AngularInputSignalWithTransform<T, U> = AngularCore.InputSignalWithTransform<T, U>;
|
|
71
79
|
type AngularOutputEmitterRef<T> = AngularCore.OutputEmitterRef<T>;
|
|
80
|
+
type AngularModelSignal<T> = AngularCore.ModelSignal<T>;
|
|
72
81
|
type AngularHasInputSignal = typeof AngularCore extends {
|
|
73
82
|
input: infer U;
|
|
74
83
|
} ? true : false;
|
|
75
84
|
type AngularHasOutputSignal = typeof AngularCore extends {
|
|
76
85
|
output: infer U;
|
|
77
86
|
} ? true : false;
|
|
87
|
+
type AngularHasModelSignal = typeof AngularCore extends {
|
|
88
|
+
model: infer U;
|
|
89
|
+
} ? true : false;
|
|
78
90
|
type InputSignal<T> = AngularHasInputSignal extends true ? AngularInputSignal<T> : never;
|
|
79
91
|
type InputSignalWithTransform<T, U> = AngularHasInputSignal extends true ? AngularInputSignalWithTransform<T, U> : never;
|
|
80
92
|
type OutputEmitterRef<T> = AngularHasOutputSignal extends true ? AngularOutputEmitterRef<T> : never;
|
|
93
|
+
type ModelSignal<T> = AngularHasModelSignal extends true ? AngularModelSignal<T> : never;
|
|
81
94
|
type TransformInputSignalType<T> = {
|
|
82
95
|
[K in keyof T]: T[K] extends InputSignal<infer E> ? E : T[K] extends InputSignalWithTransform<any, infer U> ? U : T[K];
|
|
83
96
|
};
|
|
84
97
|
type TransformOutputSignalType<T> = {
|
|
85
98
|
[K in keyof T]: T[K] extends OutputEmitterRef<infer E> ? (e: E) => void : T[K];
|
|
86
99
|
};
|
|
100
|
+
type TransformModelSignalType<T> = {
|
|
101
|
+
[K in keyof T]: T[K] extends ModelSignal<infer E> ? E : T[K];
|
|
102
|
+
} & {
|
|
103
|
+
[K in keyof T as T[K] extends ModelSignal<infer _E> ? `${K & string}Change` : never]: T[K] extends ModelSignal<infer E> ? (e: E) => void : never;
|
|
104
|
+
};
|
|
87
105
|
type TransformEventType<T> = {
|
|
88
106
|
[K in keyof T]: T[K] extends AngularCore.EventEmitter<infer E> ? (e: E) => void : T[K];
|
|
89
107
|
};
|
|
@@ -110,99 +128,390 @@ type TransformEventType<T> = {
|
|
|
110
128
|
*/
|
|
111
129
|
declare function setProjectAnnotations(projectAnnotations: NamedOrDefaultProjectAnnotations<any> | NamedOrDefaultProjectAnnotations<any>[]): NormalizedProjectAnnotations<AngularRenderer>;
|
|
112
130
|
|
|
113
|
-
declare global {
|
|
114
|
-
interface SymbolConstructor {
|
|
115
|
-
readonly observable: symbol;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
131
|
/**
|
|
120
|
-
|
|
132
|
+
Convert a union type to an intersection type.
|
|
121
133
|
|
|
122
|
-
|
|
123
|
-
|
|
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
|
|
124
147
|
*/
|
|
125
|
-
type
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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;
|
|
130
164
|
|
|
131
165
|
/**
|
|
132
|
-
|
|
166
|
+
Create a union of all keys from a given type, even those exclusive to specific union members.
|
|
133
167
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
|
137
171
|
|
|
138
172
|
@example
|
|
139
173
|
```
|
|
140
|
-
type
|
|
141
|
-
|
|
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'
|
|
142
198
|
```
|
|
143
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
|
+
|
|
144
213
|
@example
|
|
145
214
|
```
|
|
146
|
-
type
|
|
147
|
-
|
|
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
|
|
148
229
|
```
|
|
149
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
|
+
|
|
150
241
|
@example
|
|
151
242
|
```
|
|
152
|
-
type
|
|
153
|
-
|
|
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
|
|
154
271
|
```
|
|
155
272
|
|
|
156
|
-
@
|
|
273
|
+
@category Type Guard
|
|
274
|
+
@category Utilities
|
|
157
275
|
*/
|
|
158
|
-
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;
|
|
159
283
|
|
|
160
284
|
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
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.
|
|
285
|
+
Extract all optional keys from the given type.
|
|
164
286
|
|
|
165
|
-
This
|
|
287
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
166
288
|
|
|
167
289
|
@example
|
|
168
290
|
```
|
|
169
|
-
import type {Except} from 'type-fest';
|
|
291
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
170
292
|
|
|
171
|
-
type
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const update1: UpdateOperation<User> = {
|
|
306
|
+
name: 'Alice',
|
|
175
307
|
};
|
|
176
308
|
|
|
177
|
-
|
|
178
|
-
|
|
309
|
+
const update2: UpdateOperation<User> = {
|
|
310
|
+
name: 'Bob',
|
|
311
|
+
luckyNumber: REMOVE_FIELD,
|
|
312
|
+
};
|
|
179
313
|
```
|
|
180
314
|
|
|
181
|
-
@category
|
|
315
|
+
@category Utilities
|
|
182
316
|
*/
|
|
183
|
-
type
|
|
184
|
-
|
|
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;
|
|
185
345
|
};
|
|
186
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
|
+
|
|
187
362
|
/**
|
|
188
|
-
|
|
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
|
|
189
414
|
*/
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
Do the simplification recursively.
|
|
415
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
193
416
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
417
|
+
/**
|
|
418
|
+
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
419
|
+
|
|
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'
|
|
198
466
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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;
|
|
206
515
|
|
|
207
516
|
/**
|
|
208
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.
|
|
@@ -249,27 +558,65 @@ const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
|
249
558
|
const someType: SomeType = literal;
|
|
250
559
|
const someInterface: SomeInterface = literal;
|
|
251
560
|
|
|
252
|
-
function fn(object: Record<string, unknown>): void
|
|
561
|
+
declare function fn(object: Record<string, unknown>): void;
|
|
253
562
|
|
|
254
563
|
fn(literal); // Good: literal object type is sealed
|
|
255
564
|
fn(someType); // Good: type is sealed
|
|
565
|
+
// @ts-expect-error
|
|
256
566
|
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
257
567
|
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
258
568
|
```
|
|
259
569
|
|
|
260
570
|
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
261
|
-
|
|
571
|
+
@see {@link SimplifyDeep}
|
|
262
572
|
@category Object
|
|
263
573
|
*/
|
|
264
|
-
type Simplify<
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
574
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
575
|
+
|
|
576
|
+
/**
|
|
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;
|
|
270
615
|
|
|
271
616
|
/**
|
|
272
|
-
|
|
617
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
618
|
+
|
|
619
|
+
This is the counterpart of `PickIndexSignature`.
|
|
273
620
|
|
|
274
621
|
Use-cases:
|
|
275
622
|
- Remove overly permissive signatures from third-party types.
|
|
@@ -283,8 +630,9 @@ It relies on the fact that an empty object (`{}`) is assignable to an object wit
|
|
|
283
630
|
```
|
|
284
631
|
const indexed: Record<string, unknown> = {}; // Allowed
|
|
285
632
|
|
|
633
|
+
// @ts-expect-error
|
|
286
634
|
const keyed: Record<'foo', unknown> = {}; // Error
|
|
287
|
-
//
|
|
635
|
+
// TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
288
636
|
```
|
|
289
637
|
|
|
290
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:
|
|
@@ -293,178 +641,455 @@ Instead of causing a type error like the above, you can also use a [conditional
|
|
|
293
641
|
type Indexed = {} extends Record<string, unknown>
|
|
294
642
|
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
295
643
|
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
296
|
-
|
|
644
|
+
|
|
645
|
+
type IndexedResult = Indexed;
|
|
646
|
+
//=> '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
297
647
|
|
|
298
648
|
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
299
|
-
?
|
|
300
|
-
:
|
|
301
|
-
|
|
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>`'
|
|
302
654
|
```
|
|
303
655
|
|
|
304
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`...
|
|
305
657
|
|
|
306
658
|
```
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
659
|
+
type OmitIndexSignature<ObjectType> = {
|
|
310
660
|
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
311
|
-
]: ObjectType[KeyType]; // ...to its original value, i.e. `
|
|
661
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
312
662
|
};
|
|
313
663
|
```
|
|
314
664
|
|
|
315
665
|
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
316
666
|
|
|
317
667
|
```
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
668
|
+
type OmitIndexSignature<ObjectType> = {
|
|
321
669
|
[KeyType in keyof ObjectType
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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>`
|
|
326
674
|
]: ObjectType[KeyType];
|
|
327
675
|
};
|
|
328
676
|
```
|
|
329
677
|
|
|
330
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.
|
|
331
679
|
|
|
332
|
-
```
|
|
333
|
-
import type {RemoveIndexSignature} from 'type-fest';
|
|
334
|
-
|
|
335
|
-
type RemoveIndexSignature<ObjectType> = {
|
|
336
|
-
[KeyType in keyof ObjectType
|
|
337
|
-
as {} extends Record<KeyType, unknown>
|
|
338
|
-
? never // => Remove this `KeyType`.
|
|
339
|
-
: KeyType // => Keep this `KeyType` as it is.
|
|
340
|
-
]: ObjectType[KeyType];
|
|
341
|
-
};
|
|
342
|
-
```
|
|
343
|
-
|
|
344
680
|
@example
|
|
345
681
|
```
|
|
346
|
-
import type {
|
|
682
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
347
683
|
|
|
348
|
-
|
|
684
|
+
type Example = {
|
|
349
685
|
// These index signatures will be removed.
|
|
350
|
-
[x: string]: any
|
|
351
|
-
[x: number]: any
|
|
352
|
-
[x: symbol]: any
|
|
353
|
-
[x: `head-${string}`]: string
|
|
354
|
-
[x: `${string}-tail`]: string
|
|
355
|
-
[x: `head-${string}-tail`]: string
|
|
356
|
-
[x: `${bigint}`]: string
|
|
357
|
-
[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;
|
|
358
694
|
|
|
359
695
|
// These explicitly defined keys will remain.
|
|
360
696
|
foo: 'bar';
|
|
361
697
|
qux?: 'baz';
|
|
362
|
-
}
|
|
698
|
+
};
|
|
363
699
|
|
|
364
|
-
type ExampleWithoutIndexSignatures =
|
|
365
|
-
|
|
700
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
701
|
+
//=> {foo: 'bar'; qux?: 'baz'}
|
|
366
702
|
```
|
|
367
703
|
|
|
704
|
+
@see {@link PickIndexSignature}
|
|
368
705
|
@category Object
|
|
369
706
|
*/
|
|
370
|
-
type
|
|
707
|
+
type OmitIndexSignature<ObjectType> = {
|
|
371
708
|
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
372
709
|
? never
|
|
373
710
|
: KeyType]: ObjectType[KeyType];
|
|
374
711
|
};
|
|
375
712
|
|
|
376
713
|
/**
|
|
377
|
-
|
|
714
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
378
715
|
|
|
379
|
-
|
|
716
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
380
717
|
|
|
381
718
|
@example
|
|
382
719
|
```
|
|
383
|
-
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';
|
|
384
777
|
|
|
385
778
|
type Foo = {
|
|
386
|
-
a:
|
|
387
|
-
b
|
|
779
|
+
a: string;
|
|
780
|
+
b: number;
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
type Bar = {
|
|
784
|
+
a: number; // Conflicts with Foo['a']
|
|
388
785
|
c: boolean;
|
|
389
|
-
}
|
|
786
|
+
};
|
|
390
787
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
//
|
|
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;
|
|
396
823
|
// }
|
|
397
824
|
```
|
|
398
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}
|
|
399
829
|
@category Object
|
|
400
830
|
*/
|
|
401
|
-
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> =
|
|
402
839
|
Simplify<
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
406
|
-
Partial<Pick<BaseType, Keys>>
|
|
840
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
841
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
407
842
|
>;
|
|
408
843
|
|
|
409
844
|
/**
|
|
410
|
-
|
|
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.
|
|
411
850
|
|
|
412
|
-
|
|
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.
|
|
413
887
|
|
|
414
888
|
@example
|
|
415
889
|
```
|
|
416
|
-
|
|
890
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
891
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
892
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
417
893
|
|
|
418
|
-
type
|
|
894
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
895
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
896
|
+
```
|
|
419
897
|
|
|
420
|
-
|
|
421
|
-
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
898
|
+
@example
|
|
422
899
|
```
|
|
900
|
+
// Complains if default values are not provided for optional options
|
|
423
901
|
|
|
424
|
-
|
|
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
|
+
```
|
|
425
910
|
|
|
426
911
|
@example
|
|
427
912
|
```
|
|
428
|
-
|
|
913
|
+
// Complains if an option's default type does not conform to the expected type
|
|
429
914
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
b1: () => undefined,
|
|
434
|
-
}
|
|
435
|
-
}
|
|
915
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
916
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
917
|
+
type SpecifiedOptions = {};
|
|
436
918
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
}
|
|
919
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
920
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
921
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
922
|
+
```
|
|
443
923
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
924
|
+
@example
|
|
925
|
+
```
|
|
926
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
447
927
|
|
|
448
|
-
type
|
|
449
|
-
|
|
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'.
|
|
450
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>>>>;
|
|
451
949
|
|
|
452
|
-
|
|
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}
|
|
453
976
|
*/
|
|
454
|
-
type
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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
|
+
>
|
|
468
1093
|
: never;
|
|
469
1094
|
|
|
470
1095
|
/**
|
|
@@ -489,7 +1114,7 @@ type UnionToIntersection<Union> = (
|
|
|
489
1114
|
declare function __definePreview<Addons extends PreviewAddon<never>[]>(input: {
|
|
490
1115
|
addons: Addons;
|
|
491
1116
|
} & ProjectAnnotations<AngularRenderer & InferTypes<Addons>>): AngularPreview<AngularRenderer & InferTypes<Addons>>;
|
|
492
|
-
type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<
|
|
1117
|
+
type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<OmitIndexSignature<DecoratorsArgs<AngularRenderer & T, Decorators>>>>;
|
|
493
1118
|
type InferComponentArgs<C extends abstract new (...args: any) => any> = Partial<TransformComponentType<InstanceType<C>>>;
|
|
494
1119
|
type InferAngularTypes<T, TArgs, Decorators> = AngularRenderer & T & {
|
|
495
1120
|
args: Simplify<InferArgs<TArgs, T, Decorators>>;
|
|
@@ -740,4 +1365,4 @@ interface AngularOptions {
|
|
|
740
1365
|
enableIvy?: boolean;
|
|
741
1366
|
}
|
|
742
1367
|
|
|
743
|
-
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 };
|