@storybook/react 10.0.0-beta.1 → 10.0.0-beta.10

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.
@@ -1,420 +0,0 @@
1
- import { ComponentType, JSX } from 'react';
2
- import { PreviewAddon, InferTypes, AddonTypes, Preview, Meta, Story } from 'storybook/internal/csf';
3
- import { WebRenderer, Canvas, ProjectAnnotations, Args, DecoratorFunction, ArgsStoryFn, ComponentAnnotations, StoryAnnotations, Renderer } from 'storybook/internal/types';
4
- import { RootOptions } from 'react-dom/client';
5
-
6
- declare global {
7
- interface SymbolConstructor {
8
- readonly observable: symbol;
9
- }
10
- }
11
-
12
- /**
13
- Returns a boolean for whether the two given types are equal.
14
-
15
- @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
16
- @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
17
- */
18
- type IsEqual<T, U> =
19
- (<G>() => G extends T ? 1 : 2) extends
20
- (<G>() => G extends U ? 1 : 2)
21
- ? true
22
- : false;
23
-
24
- /**
25
- Filter out keys from an object.
26
-
27
- Returns `never` if `Exclude` is strictly equal to `Key`.
28
- Returns `never` if `Key` extends `Exclude`.
29
- Returns `Key` otherwise.
30
-
31
- @example
32
- ```
33
- type Filtered = Filter<'foo', 'foo'>;
34
- //=> never
35
- ```
36
-
37
- @example
38
- ```
39
- type Filtered = Filter<'bar', string>;
40
- //=> never
41
- ```
42
-
43
- @example
44
- ```
45
- type Filtered = Filter<'bar', 'foo'>;
46
- //=> 'bar'
47
- ```
48
-
49
- @see {Except}
50
- */
51
- type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
52
-
53
- /**
54
- Create a type from an object type without certain keys.
55
-
56
- 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.
57
-
58
- 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)).
59
-
60
- @example
61
- ```
62
- import type {Except} from 'type-fest';
63
-
64
- type Foo = {
65
- a: number;
66
- b: string;
67
- c: boolean;
68
- };
69
-
70
- type FooWithoutA = Except<Foo, 'a' | 'c'>;
71
- //=> {b: string};
72
- ```
73
-
74
- @category Object
75
- */
76
- type Except<ObjectType, KeysType extends keyof ObjectType> = {
77
- [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
78
- };
79
-
80
- /**
81
- @see Simplify
82
- */
83
- interface SimplifyOptions {
84
- /**
85
- Do the simplification recursively.
86
-
87
- @default false
88
- */
89
- deep?: boolean;
90
- }
91
-
92
- // Flatten a type without worrying about the result.
93
- type Flatten<
94
- AnyType,
95
- Options extends SimplifyOptions = {},
96
- > = Options['deep'] extends true
97
- ? {[KeyType in keyof AnyType]: Simplify<AnyType[KeyType], Options>}
98
- : {[KeyType in keyof AnyType]: AnyType[KeyType]};
99
-
100
- /**
101
- 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.
102
-
103
- @example
104
- ```
105
- import type {Simplify} from 'type-fest';
106
-
107
- type PositionProps = {
108
- top: number;
109
- left: number;
110
- };
111
-
112
- type SizeProps = {
113
- width: number;
114
- height: number;
115
- };
116
-
117
- // In your editor, hovering over `Props` will show a flattened object with all the properties.
118
- type Props = Simplify<PositionProps & SizeProps>;
119
- ```
120
-
121
- Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
122
-
123
- If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
124
-
125
- @example
126
- ```
127
- import type {Simplify} from 'type-fest';
128
-
129
- interface SomeInterface {
130
- foo: number;
131
- bar?: string;
132
- baz: number | undefined;
133
- }
134
-
135
- type SomeType = {
136
- foo: number;
137
- bar?: string;
138
- baz: number | undefined;
139
- };
140
-
141
- const literal = {foo: 123, bar: 'hello', baz: 456};
142
- const someType: SomeType = literal;
143
- const someInterface: SomeInterface = literal;
144
-
145
- function fn(object: Record<string, unknown>): void {}
146
-
147
- fn(literal); // Good: literal object type is sealed
148
- fn(someType); // Good: type is sealed
149
- fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
150
- fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
151
- ```
152
-
153
- @link https://github.com/microsoft/TypeScript/issues/15300
154
-
155
- @category Object
156
- */
157
- type Simplify<
158
- AnyType,
159
- Options extends SimplifyOptions = {},
160
- > = Flatten<AnyType> extends AnyType
161
- ? Flatten<AnyType, Options>
162
- : AnyType;
163
-
164
- /**
165
- Remove any index signatures from the given object type, so that only explicitly defined properties remain.
166
-
167
- Use-cases:
168
- - Remove overly permissive signatures from third-party types.
169
-
170
- This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
171
-
172
- 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>`.
173
-
174
- (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
175
-
176
- ```
177
- const indexed: Record<string, unknown> = {}; // Allowed
178
-
179
- const keyed: Record<'foo', unknown> = {}; // Error
180
- // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
181
- ```
182
-
183
- 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:
184
-
185
- ```
186
- type Indexed = {} extends Record<string, unknown>
187
- ? '✅ `{}` is assignable to `Record<string, unknown>`'
188
- : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
189
- // => '✅ `{}` is assignable to `Record<string, unknown>`'
190
-
191
- type Keyed = {} extends Record<'foo' | 'bar', unknown>
192
- ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
193
- : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
194
- // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
195
- ```
196
-
197
- 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`...
198
-
199
- ```
200
- import type {RemoveIndexSignature} from 'type-fest';
201
-
202
- type RemoveIndexSignature<ObjectType> = {
203
- [KeyType in keyof ObjectType // Map each key of `ObjectType`...
204
- ]: ObjectType[KeyType]; // ...to its original value, i.e. `RemoveIndexSignature<Foo> == Foo`.
205
- };
206
- ```
207
-
208
- ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
209
-
210
- ```
211
- import type {RemoveIndexSignature} from 'type-fest';
212
-
213
- type RemoveIndexSignature<ObjectType> = {
214
- [KeyType in keyof ObjectType
215
- // Is `{}` assignable to `Record<KeyType, unknown>`?
216
- as {} extends Record<KeyType, unknown>
217
- ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
218
- : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
219
- ]: ObjectType[KeyType];
220
- };
221
- ```
222
-
223
- 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.
224
-
225
- ```
226
- import type {RemoveIndexSignature} from 'type-fest';
227
-
228
- type RemoveIndexSignature<ObjectType> = {
229
- [KeyType in keyof ObjectType
230
- as {} extends Record<KeyType, unknown>
231
- ? never // => Remove this `KeyType`.
232
- : KeyType // => Keep this `KeyType` as it is.
233
- ]: ObjectType[KeyType];
234
- };
235
- ```
236
-
237
- @example
238
- ```
239
- import type {RemoveIndexSignature} from 'type-fest';
240
-
241
- interface Example {
242
- // These index signatures will be removed.
243
- [x: string]: any
244
- [x: number]: any
245
- [x: symbol]: any
246
- [x: `head-${string}`]: string
247
- [x: `${string}-tail`]: string
248
- [x: `head-${string}-tail`]: string
249
- [x: `${bigint}`]: string
250
- [x: `embedded-${number}`]: string
251
-
252
- // These explicitly defined keys will remain.
253
- foo: 'bar';
254
- qux?: 'baz';
255
- }
256
-
257
- type ExampleWithoutIndexSignatures = RemoveIndexSignature<Example>;
258
- // => { foo: 'bar'; qux?: 'baz' | undefined; }
259
- ```
260
-
261
- @category Object
262
- */
263
- type RemoveIndexSignature<ObjectType> = {
264
- [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
265
- ? never
266
- : KeyType]: ObjectType[KeyType];
267
- };
268
-
269
- /**
270
- Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
271
-
272
- 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.
273
-
274
- @example
275
- ```
276
- import type {SetOptional} from 'type-fest';
277
-
278
- type Foo = {
279
- a: number;
280
- b?: string;
281
- c: boolean;
282
- }
283
-
284
- type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
285
- // type SomeOptional = {
286
- // a: number;
287
- // b?: string; // Was already optional and still is.
288
- // c?: boolean; // Is now optional.
289
- // }
290
- ```
291
-
292
- @category Object
293
- */
294
- type SetOptional<BaseType, Keys extends keyof BaseType> =
295
- Simplify<
296
- // Pick just the keys that are readonly from the base type.
297
- Except<BaseType, Keys> &
298
- // Pick the keys that should be mutable from the base type and make them mutable.
299
- Partial<Pick<BaseType, Keys>>
300
- >;
301
-
302
- /**
303
- Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
304
-
305
- Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
306
-
307
- @example
308
- ```
309
- import type {UnionToIntersection} from 'type-fest';
310
-
311
- type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
312
-
313
- type Intersection = UnionToIntersection<Union>;
314
- //=> {the(): void; great(arg: string): void; escape: boolean};
315
- ```
316
-
317
- A more applicable example which could make its way into your library code follows.
318
-
319
- @example
320
- ```
321
- import type {UnionToIntersection} from 'type-fest';
322
-
323
- class CommandOne {
324
- commands: {
325
- a1: () => undefined,
326
- b1: () => undefined,
327
- }
328
- }
329
-
330
- class CommandTwo {
331
- commands: {
332
- a2: (argA: string) => undefined,
333
- b2: (argB: string) => undefined,
334
- }
335
- }
336
-
337
- const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
338
- type Union = typeof union;
339
- //=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
340
-
341
- type Intersection = UnionToIntersection<Union>;
342
- //=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
343
- ```
344
-
345
- @category Type
346
- */
347
- type UnionToIntersection<Union> = (
348
- // `extends unknown` is always going to be the case and is used to convert the
349
- // `Union` into a [distributive conditional
350
- // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
351
- Union extends unknown
352
- // The union type is used as the only argument to a function since the union
353
- // of function arguments is an intersection.
354
- ? (distributedUnion: Union) => void
355
- // This won't happen.
356
- : never
357
- // Infer the `Intersection` type since TypeScript represents the positional
358
- // arguments of unions of functions as an intersection of the union.
359
- ) extends ((mergedIntersection: infer Intersection) => void)
360
- ? Intersection
361
- : never;
362
-
363
- interface ReactRenderer extends WebRenderer {
364
- component: ComponentType<this['T']>;
365
- storyResult: StoryFnReactReturnType;
366
- mount: (ui?: JSX.Element) => Promise<Canvas>;
367
- }
368
- interface ReactParameters {
369
- /** React renderer configuration */
370
- react?: {
371
- /**
372
- * Whether to enable React Server Components
373
- *
374
- * @see https://storybook.js.org/docs/get-started/frameworks/nextjs#react-server-components-rsc
375
- */
376
- rsc?: boolean;
377
- /** Options passed to React root creation */
378
- rootOptions?: RootOptions;
379
- };
380
- }
381
- interface ReactTypes extends ReactRenderer {
382
- parameters: ReactParameters;
383
- }
384
- type StoryFnReactReturnType = JSX.Element;
385
-
386
- type AddMocks<TArgs, DefaultArgs> = Simplify<{
387
- [T in keyof TArgs]: T extends keyof DefaultArgs ? DefaultArgs[T] extends (...args: any) => any & {
388
- mock: {};
389
- } ? DefaultArgs[T] : TArgs[T] : TArgs[T];
390
- }>;
391
-
392
- declare function __definePreview<Addons extends PreviewAddon<never>[]>(input: {
393
- addons: Addons;
394
- } & ProjectAnnotations<ReactTypes & InferTypes<Addons>>): ReactPreview<ReactTypes & InferTypes<Addons>>;
395
- interface ReactPreview<T extends AddonTypes> extends Preview<ReactTypes & T> {
396
- meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactTypes & T, any>, TMetaArgs extends Partial<TArgs>>(meta: {
397
- render?: ArgsStoryFn<ReactTypes & T, TArgs>;
398
- component?: ComponentType<TArgs>;
399
- decorators?: Decorators | Decorators[];
400
- args?: TMetaArgs;
401
- } & Omit<ComponentAnnotations<ReactTypes & T, TArgs>, 'decorators' | 'component' | 'args' | 'render'>): ReactMeta<ReactTypes & T & {
402
- args: Simplify<TArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactTypes & T, Decorators>>>>;
403
- }, {
404
- args: Partial<TArgs> extends TMetaArgs ? {} : TMetaArgs;
405
- }>;
406
- }
407
- type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
408
- interface ReactMeta<T extends ReactTypes, MetaInput extends ComponentAnnotations<T>> extends Meta<T, MetaInput> {
409
- story<TInput extends (() => ReactTypes['storyResult']) | (StoryAnnotations<T, T['args']> & {
410
- render: () => ReactTypes['storyResult'];
411
- })>(story?: TInput): ReactStory<T, TInput extends () => ReactTypes['storyResult'] ? {
412
- render: TInput;
413
- } : TInput>;
414
- story<TInput extends Simplify<StoryAnnotations<T, AddMocks<T['args'], MetaInput['args']>, SetOptional<T['args'], keyof T['args'] & keyof MetaInput['args']>>>>(story?: TInput): ReactStory<T, TInput>;
415
- }
416
- interface ReactStory<T extends ReactTypes, TInput extends StoryAnnotations<T, T['args']>> extends Story<T, TInput> {
417
- Component: ComponentType<Partial<T['args']>>;
418
- }
419
-
420
- export { type ReactPreview, type ReactStory, __definePreview };