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