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 type inference bug with decorators #19839

Merged
merged 1 commit into from
Nov 16, 2022
Merged
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: 17 additions & 3 deletions code/renderers/react/src/public-types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, test } from '@jest/globals';

import { satisfies } from '@storybook/core-common';
import type { StoryAnnotations } from '@storybook/types';
import type { Args, StoryAnnotations, StrictArgs } from '@storybook/types';
import { expectTypeOf } from 'expect-type';
import type { KeyboardEventHandler, ReactNode } from 'react';
import React from 'react';
Expand All @@ -11,7 +11,7 @@ import type { SetOptional } from 'type-fest';
import type { Decorator, Meta, StoryObj } from './public-types';
import type { ReactRenderer } from './types';

type ReactStory<Args, RequiredArgs> = StoryAnnotations<ReactRenderer, Args, RequiredArgs>;
type ReactStory<TArgs, TRequiredArgs> = StoryAnnotations<ReactRenderer, TArgs, TRequiredArgs>;

type ButtonProps = { label: string; disabled: boolean };
const Button: (props: ButtonProps) => JSX.Element = () => <></>;
Expand Down Expand Up @@ -172,10 +172,24 @@ describe('Story args can be inferred', () => {
</>
);

// decorator is not using args
const thirdDecorator: Decorator<Args> = (Story) => (
<>
<Story />
</>
);

// decorator is not using args
const fourthDecorator: Decorator<StrictArgs> = (Story) => (
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
<>
<Story />
</>
);

const meta = satisfies<Meta<Props>>()({
component: Button,
args: { disabled: false },
decorators: [withDecorator, secondDecorator],
decorators: [withDecorator, secondDecorator, thirdDecorator, fourthDecorator],
});

const Basic: StoryObj<typeof meta> = {
Expand Down
13 changes: 10 additions & 3 deletions code/renderers/react/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,22 @@ export type StoryObj<TMetaOrCmpOrArgs = Args> = TMetaOrCmpOrArgs extends {
? StoryAnnotations<
ReactRenderer,
TArgs,
SetOptional<TArgs, Extract<keyof TArgs, keyof (DefaultArgs & ActionArgs<TArgs>)>>
SetOptional<TArgs, keyof TArgs & keyof (DefaultArgs & ActionArgs<TArgs>)>
>
: never
: TMetaOrCmpOrArgs extends ComponentType<any>
? StoryAnnotations<ReactRenderer, ComponentProps<TMetaOrCmpOrArgs>>
: StoryAnnotations<ReactRenderer, TMetaOrCmpOrArgs>;

type ActionArgs<RArgs> = {
[P in keyof RArgs as ((...args: any[]) => void) extends RArgs[P] ? P : never]: RArgs[P];
type ActionArgs<TArgs> = {
// This can be read as: filter TArgs on functions where we can assign a void function to that function.
// The docs addon argsEnhancers can only safely provide a default value for void functions.
// Other kind of required functions should be provided by the user.
[P in keyof TArgs as TArgs[P] extends (...args: any[]) => any
? ((...args: any[]) => void) extends TArgs[P]
? P
: never
: never]: TArgs[P];
};

/**
Expand Down