@storybook/react 8.6.0-alpha.4 → 8.6.0-beta.0

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.mjs CHANGED
@@ -1,4 +1,6 @@
1
+ export { definePreview } from './chunk-DWFEPKAE.mjs';
1
2
  import { entry_preview_exports, renderToCanvas } from './chunk-MHYVTTFG.mjs';
3
+ import './chunk-EWIU6LHT.mjs';
2
4
  import './chunk-XP5HYGXS.mjs';
3
5
  import { global } from '@storybook/global';
4
6
  import * as React from 'react';
@@ -0,0 +1,196 @@
1
+ import { ComponentType } from 'react';
2
+ import { Preview, Meta, Story } from 'storybook/internal/csf';
3
+ import { Args, DecoratorFunction, ArgsStoryFn, ComponentAnnotations, Renderer, StoryAnnotations } from 'storybook/internal/types';
4
+ import { AddMocks } from 'src/public-types';
5
+ import { S as Simplify, a as SetOptional } from './set-optional.d-c7228b0c.js';
6
+ import { R as ReactRenderer } from './types-5617c98e.js';
7
+
8
+ /**
9
+ Remove any index signatures from the given object type, so that only explicitly defined properties remain.
10
+
11
+ Use-cases:
12
+ - Remove overly permissive signatures from third-party types.
13
+
14
+ This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
15
+
16
+ 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>`.
17
+
18
+ (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
19
+
20
+ ```
21
+ const indexed: Record<string, unknown> = {}; // Allowed
22
+
23
+ const keyed: Record<'foo', unknown> = {}; // Error
24
+ // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
25
+ ```
26
+
27
+ 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:
28
+
29
+ ```
30
+ type Indexed = {} extends Record<string, unknown>
31
+ ? '✅ `{}` is assignable to `Record<string, unknown>`'
32
+ : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
33
+ // => '✅ `{}` is assignable to `Record<string, unknown>`'
34
+
35
+ type Keyed = {} extends Record<'foo' | 'bar', unknown>
36
+ ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
37
+ : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
38
+ // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
39
+ ```
40
+
41
+ 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`...
42
+
43
+ ```
44
+ import type {RemoveIndexSignature} from 'type-fest';
45
+
46
+ type RemoveIndexSignature<ObjectType> = {
47
+ [KeyType in keyof ObjectType // Map each key of `ObjectType`...
48
+ ]: ObjectType[KeyType]; // ...to its original value, i.e. `RemoveIndexSignature<Foo> == Foo`.
49
+ };
50
+ ```
51
+
52
+ ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
53
+
54
+ ```
55
+ import type {RemoveIndexSignature} from 'type-fest';
56
+
57
+ type RemoveIndexSignature<ObjectType> = {
58
+ [KeyType in keyof ObjectType
59
+ // Is `{}` assignable to `Record<KeyType, unknown>`?
60
+ as {} extends Record<KeyType, unknown>
61
+ ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
62
+ : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
63
+ ]: ObjectType[KeyType];
64
+ };
65
+ ```
66
+
67
+ 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.
68
+
69
+ ```
70
+ import type {RemoveIndexSignature} from 'type-fest';
71
+
72
+ type RemoveIndexSignature<ObjectType> = {
73
+ [KeyType in keyof ObjectType
74
+ as {} extends Record<KeyType, unknown>
75
+ ? never // => Remove this `KeyType`.
76
+ : KeyType // => Keep this `KeyType` as it is.
77
+ ]: ObjectType[KeyType];
78
+ };
79
+ ```
80
+
81
+ @example
82
+ ```
83
+ import type {RemoveIndexSignature} from 'type-fest';
84
+
85
+ interface Example {
86
+ // These index signatures will be removed.
87
+ [x: string]: any
88
+ [x: number]: any
89
+ [x: symbol]: any
90
+ [x: `head-${string}`]: string
91
+ [x: `${string}-tail`]: string
92
+ [x: `head-${string}-tail`]: string
93
+ [x: `${bigint}`]: string
94
+ [x: `embedded-${number}`]: string
95
+
96
+ // These explicitly defined keys will remain.
97
+ foo: 'bar';
98
+ qux?: 'baz';
99
+ }
100
+
101
+ type ExampleWithoutIndexSignatures = RemoveIndexSignature<Example>;
102
+ // => { foo: 'bar'; qux?: 'baz' | undefined; }
103
+ ```
104
+
105
+ @category Object
106
+ */
107
+ type RemoveIndexSignature<ObjectType> = {
108
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
109
+ ? never
110
+ : KeyType]: ObjectType[KeyType];
111
+ };
112
+
113
+ /**
114
+ 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).
115
+
116
+ Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
117
+
118
+ @example
119
+ ```
120
+ import type {UnionToIntersection} from 'type-fest';
121
+
122
+ type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
123
+
124
+ type Intersection = UnionToIntersection<Union>;
125
+ //=> {the(): void; great(arg: string): void; escape: boolean};
126
+ ```
127
+
128
+ A more applicable example which could make its way into your library code follows.
129
+
130
+ @example
131
+ ```
132
+ import type {UnionToIntersection} from 'type-fest';
133
+
134
+ class CommandOne {
135
+ commands: {
136
+ a1: () => undefined,
137
+ b1: () => undefined,
138
+ }
139
+ }
140
+
141
+ class CommandTwo {
142
+ commands: {
143
+ a2: (argA: string) => undefined,
144
+ b2: (argB: string) => undefined,
145
+ }
146
+ }
147
+
148
+ const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
149
+ type Union = typeof union;
150
+ //=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
151
+
152
+ type Intersection = UnionToIntersection<Union>;
153
+ //=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
154
+ ```
155
+
156
+ @category Type
157
+ */
158
+ type UnionToIntersection<Union> = (
159
+ // `extends unknown` is always going to be the case and is used to convert the
160
+ // `Union` into a [distributive conditional
161
+ // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
162
+ Union extends unknown
163
+ // The union type is used as the only argument to a function since the union
164
+ // of function arguments is an intersection.
165
+ ? (distributedUnion: Union) => void
166
+ // This won't happen.
167
+ : never
168
+ // Infer the `Intersection` type since TypeScript represents the positional
169
+ // arguments of unions of functions as an intersection of the union.
170
+ ) extends ((mergedIntersection: infer Intersection) => void)
171
+ ? Intersection
172
+ : never;
173
+
174
+ declare function definePreview(preview: ReactPreview['input']): ReactPreview;
175
+ interface ReactPreview extends Preview<ReactRenderer> {
176
+ meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactRenderer, any>, TMetaArgs extends Partial<TArgs>>(meta: {
177
+ render?: ArgsStoryFn<ReactRenderer, TArgs>;
178
+ component?: ComponentType<TArgs>;
179
+ decorators?: Decorators | Decorators[];
180
+ args?: TMetaArgs;
181
+ } & Omit<ComponentAnnotations<ReactRenderer, TArgs>, 'decorators'>): ReactMeta<{
182
+ args: Simplify<TArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactRenderer, Decorators>>>>;
183
+ }, {
184
+ args: Partial<TArgs> extends TMetaArgs ? {} : TMetaArgs;
185
+ }>;
186
+ }
187
+ type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
188
+ interface ReactMeta<Context extends {
189
+ args: Args;
190
+ }, MetaInput extends ComponentAnnotations<ReactRenderer>> extends Meta<ReactRenderer, Context['args']> {
191
+ story<const TInput extends Simplify<StoryAnnotations<ReactRenderer, AddMocks<Context['args'], MetaInput['args']>, SetOptional<Context['args'], keyof Context['args'] & keyof MetaInput['args']>>>>(story: TInput): ReactStory;
192
+ }
193
+ interface ReactStory extends Story<ReactRenderer> {
194
+ }
195
+
196
+ export { ReactPreview, definePreview };