Rework composedStory types

This commit is contained in:
Tom Coleman 2022-11-22 16:58:16 +11:00
parent b2e5b71dfd
commit 6b44b1fa63
4 changed files with 26 additions and 40 deletions

View File

@ -5,8 +5,8 @@ import type {
ComponentAnnotations,
LegacyStoryAnnotationsOrFn,
ProjectAnnotations,
Store_ComposedStoryPlayFn,
Store_ComposeStory,
ComposedStoryPlayFn,
ComposeStory,
Store_CSFExports,
StoryContext,
Parameters,
@ -85,7 +85,7 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend
composedStory.storyName = storyName;
composedStory.args = story.initialArgs;
composedStory.play = story.playFunction as Store_ComposedStoryPlayFn;
composedStory.play = story.playFunction as ComposedStoryPlayFn;
composedStory.parameters = story.parameters as Parameters;
return composedStory;
@ -94,7 +94,7 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend
export function composeStories<TModule extends Store_CSFExports>(
storiesImport: TModule,
globalConfig: ProjectAnnotations<Renderer>,
composeStoryFn: Store_ComposeStory
composeStoryFn: ComposeStory
) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { default: meta, __esModule, __namedExportsOrder, ...stories } = storiesImport;

View File

@ -1,20 +1,15 @@
import mapValues from 'lodash/mapValues';
import { logger } from '@storybook/client-logger';
import type {
Renderer,
ArgTypesEnhancer,
SBEnumType,
Store_ControlsMatchers,
StrictInputType,
} from '@storybook/types';
import type { Renderer, ArgTypesEnhancer, SBEnumType, StrictInputType } from '@storybook/types';
import { filterArgTypes } from './filterArgTypes';
import { combineParameters } from './parameters';
const inferControl = (
argType: StrictInputType,
name: string,
matchers: Store_ControlsMatchers
): any => {
export type ControlsMatchers = {
date: RegExp;
color: RegExp;
};
const inferControl = (argType: StrictInputType, name: string, matchers: ControlsMatchers): any => {
const { type, options } = argType;
if (!type) {
return undefined;

View File

@ -14,25 +14,24 @@ import type {
import type { ProjectAnnotations } from './story';
// TODO -- I think the name "CSFExports" overlaps here a bit with the types in csfFile.ts
// we might want to reconcile @yannbf
export type Store_CSFExports<TRenderer extends Renderer = Renderer> = {
default: ComponentAnnotations<TRenderer, Args>;
__esModule?: boolean;
__namedExportsOrder?: string[];
};
export type Store_ComposedStoryPlayContext = Partial<StoryContext> &
Pick<StoryContext, 'canvasElement'>;
export type ComposedStoryPlayContext = Partial<StoryContext> & Pick<StoryContext, 'canvasElement'>;
export type Store_ComposedStoryPlayFn = (
context: Store_ComposedStoryPlayContext
) => Promise<void> | void;
export type ComposedStoryPlayFn = (context: ComposedStoryPlayContext) => Promise<void> | void;
export type PreparedStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = AnnotatedStoryFn<
TRenderer,
TArgs
> & { play: Store_ComposedStoryPlayFn };
> & { play: ComposedStoryPlayFn };
export type Store_ComposedStory<TRenderer extends Renderer = Renderer, TArgs = Args> =
export type ComposedStory<TRenderer extends Renderer = Renderer, TArgs = Args> =
| StoryFn<TRenderer, TArgs>
| StoryAnnotations<TRenderer, TArgs>;
@ -42,23 +41,15 @@ export type Store_ComposedStory<TRenderer extends Renderer = Renderer, TArgs = A
* 2. infer the actual prop type for each Story
* 3. reconstruct Story with Partial. Story<Props> -> Story<Partial<Props>>
*/
export type Store_StoriesWithPartialProps<TRenderer extends Renderer, TModule> = {
export type StoriesWithPartialProps<TRenderer extends Renderer, TModule> = {
// @TODO once we can use Typescript 4.0 do this to exclude nonStory exports:
// replace [K in keyof TModule] with [K in keyof TModule as TModule[K] extends ComposedStory<any> ? K : never]
[K in keyof TModule]: TModule[K] extends Store_ComposedStory<infer _, infer TProps>
[K in keyof TModule]: TModule[K] extends ComposedStory<infer _, infer TProps>
? AnnotatedStoryFn<TRenderer, Partial<TProps>>
: unknown;
};
export type Store_ControlsMatchers = {
date: RegExp;
color: RegExp;
};
export interface Store_ComposeStory<
TRenderer extends Renderer = Renderer,
TArgs extends Args = Args
> {
export interface ComposeStory<TRenderer extends Renderer = Renderer, TArgs extends Args = Args> {
(
storyAnnotations: AnnotatedStoryFn<TRenderer, TArgs> | StoryAnnotations<TRenderer, TArgs>,
componentAnnotations: ComponentAnnotations<TRenderer, TArgs>,
@ -68,7 +59,7 @@ export interface Store_ComposeStory<
(extraArgs: Partial<TArgs>): TRenderer['storyResult'];
storyName: string;
args: Args;
play: Store_ComposedStoryPlayFn;
play: ComposedStoryPlayFn;
parameters: Parameters;
};
}

View File

@ -6,9 +6,9 @@ import {
import type {
Args,
ProjectAnnotations,
Store_ComposedStory,
ComposedStory,
Store_CSFExports,
Store_StoriesWithPartialProps,
StoriesWithPartialProps,
} from '@storybook/types';
import { deprecate } from '@storybook/client-logger';
@ -81,13 +81,13 @@ const defaultProjectAnnotations: ProjectAnnotations<ReactRenderer> = {
* @param [exportsName] - in case your story does not contain a name and you want it to have a name.
*/
export function composeStory<TArgs extends Args = Args>(
story: Store_ComposedStory<ReactRenderer, TArgs>,
story: ComposedStory<ReactRenderer, TArgs>,
componentAnnotations: Meta<TArgs | any>,
projectAnnotations?: ProjectAnnotations<ReactRenderer>,
exportsName?: string
) {
return originalComposeStory<ReactRenderer, TArgs>(
story as Store_ComposedStory<ReactRenderer, Args>,
story as ComposedStory<ReactRenderer, Args>,
componentAnnotations,
projectAnnotations,
defaultProjectAnnotations,
@ -128,7 +128,7 @@ export function composeStories<TModule extends Store_CSFExports<ReactRenderer>>(
const composedStories = originalComposeStories(csfExports, projectAnnotations, composeStory);
return composedStories as unknown as Omit<
Store_StoriesWithPartialProps<ReactRenderer, TModule>,
StoriesWithPartialProps<ReactRenderer, TModule>,
keyof Store_CSFExports
>;
}