@storybook/preact 10.4.1 → 10.4.3

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