Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: story ComponentAnnotations['subcomponents'] to correctly use its own type for subcomponents rather than attempt to inherit from the component #103

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/story.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/* global HTMLElement */
import { expectTypeOf } from 'expect-type';
import {
Renderer,
Args,
ArgsFromMeta,
ArgsStoryFn,
ComponentAnnotations,
DecoratorFunction,
LoaderFunction,
ProjectAnnotations,
Renderer,
StoryAnnotationsOrFn,
StrictArgs,
} from './story.js';
Expand All @@ -34,6 +34,8 @@ type ButtonArgs = {

const Button = (props: ButtonArgs) => 'Button';

const ButtonIcon = ({ icon }: { icon: string }) => `ButtonIcon ${icon}`;

let a = 1;
async function doSomething() {
a = 2;
Expand Down Expand Up @@ -110,6 +112,22 @@ CSF1Story.story = {
args: { a: 1 },
};

const simpleWithSubComponents: XMeta = {
title: 'simple',
component: Button,
tags: ['foo', 'bar'],
decorators: [(storyFn, context) => `withDecorator(${storyFn(context)})`],
parameters: { a: () => null, b: NaN, c: Symbol('symbol') },
loaders: [() => Promise.resolve({ d: '3' })],
async beforeEach() {
await doSomething();
return cleanup;
},
args: { x: '1' },
argTypes: { x: { type: { name: 'string' } } },
subcomponents: { ButtonIcon }, // the fact that this line does not cause a Typescript compilation error itself means that the type is correct
};

const CSF2Story: XStory = () => 'Named Story';
CSF2Story.storyName = 'Another name for story';
CSF2Story.tags = ['foo', 'bar'];
Expand Down
2 changes: 1 addition & 1 deletion src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ export interface ComponentAnnotations<TRenderer extends Renderer = Renderer, TAr
*
* By defining them each component will have its tab in the args table.
*/
subcomponents?: Record<string, TRenderer['component']>;
subcomponents?: Record<string, (TRenderer & { T: any })['component']>;

/**
* Function that is executed after the story is rendered.
Expand Down