Skip to content

Commit

Permalink
use StoryAnnotationsOrFn instead
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Aug 10, 2023
1 parent 7bc6b51 commit bb74e0b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
16 changes: 4 additions & 12 deletions code/lib/types/src/modules/composedStory.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable @typescript-eslint/naming-convention */

import type { Renderer, StoryId } from '@storybook/csf';
import type { PartialStoryFn, Renderer, StoryId } from '@storybook/csf';

import type {
AnnotatedStoryFn,
Args,
ComponentAnnotations,
Parameters,
StoryAnnotations,
StoryAnnotationsOrFn,
StoryContext,
StoryFn,
} from './csf';

import type { ProjectAnnotations } from './story';
Expand Down Expand Up @@ -61,14 +61,6 @@ export type ComposedStoryFn<
storyName: string;
parameters: Parameters;
};

/**
* Type that matches whether the story is a CSF2 story or a CSF3 story, used for assertion and inference
*/
export type StoryLike<TRenderer extends Renderer = Renderer, TArgs = Args> =
| StoryFn<TRenderer, TArgs>
| StoryAnnotations<TRenderer, TArgs>;

/**
* Based on a module of stories, it returns all stories within it, filtering non-stories
* Each story will have partial props, as their props should be handled when composing stories
Expand All @@ -78,9 +70,9 @@ export type StoriesWithPartialProps<TRenderer extends Renderer, TModule> = {
// 1. pick the keys K of T that have properties that are Story<AnyProps>
// 2. infer the actual prop type for each Story
// 3. reconstruct Story with Partial. Story<Props> -> Story<Partial<Props>>
[K in keyof TModule as TModule[K] extends StoryLike<infer _, infer _TProps>
[K in keyof TModule as TModule[K] extends StoryAnnotationsOrFn<infer _, infer _TProps>
? K
: never]: TModule[K] extends StoryLike<infer _, infer TProps>
: never]: TModule[K] extends StoryAnnotationsOrFn<infer _, infer TProps>
? ComposedStoryFn<TRenderer, Partial<TProps>>
: unknown;
};
Expand Down
6 changes: 3 additions & 3 deletions code/renderers/react/src/testing-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import type {
Args,
ProjectAnnotations,
StoryLike,
StoryAnnotationsOrFn,
Store_CSFExports,
StoriesWithPartialProps,
} from '@storybook/types';
Expand Down Expand Up @@ -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: StoryLike<ReactRenderer, TArgs>,
story: StoryAnnotationsOrFn<ReactRenderer, TArgs>,
componentAnnotations: Meta<TArgs | any>,
projectAnnotations?: ProjectAnnotations<ReactRenderer>,
exportsName?: string
) {
return originalComposeStory<ReactRenderer, TArgs>(
story as StoryLike<ReactRenderer, Args>,
story as StoryAnnotationsOrFn<ReactRenderer, Args>,
componentAnnotations,
projectAnnotations,
defaultProjectAnnotations,
Expand Down

0 comments on commit bb74e0b

Please sign in to comment.