csf icon indicating copy to clipboard operation
csf copied to clipboard

fix: Explicitly allow undefined type for table.type.summary

Open Hyzual opened this issue 1 year ago • 5 comments

Closes storybookjs/storybook#27129

Allowing explicitly undefined as type for table.type.summary lets Story authors disable the automatic type summary while having strict TypeScript options. With exactOptionalPropertyTypes enforced, using undefined as a type was forbidden, as TypeScript would raise an error. With this change, it should be allowed.

Hyzual avatar Jun 11 '24 09:06 Hyzual

Did you temporarily turn off exactOptionalPropertyTypes and see if passing undefined behaved like you expect? I don't have that setting turned on, and when I pass undefined for summary, it defaults back to the automatic type definition.

I then tried null as any and that worked as expected. So I'd think we'd need to allow null instead of undefined for this to actually fix the issue.

Maybe I'm missing something else?

dcneiner avatar Jun 21 '24 00:06 dcneiner

Yes, we turned off exactOptionalPropertyTypes for the Storybook part of our repository. Writing the following does remove the automatic type definition like we wanted to in Storybook:

table: {
  type: { summary: undefined },
},

We used to pass null instead of undefined, but since the upgrade to Storybook v8.1, the typechecking would not allow it and so we switched to undefined which had the same result for us.

Hyzual avatar Jun 24 '24 09:06 Hyzual

Thank you @Hyzual – I'll see what else I may have different or what I might have missed.

dcneiner avatar Jun 24 '24 13:06 dcneiner

OK, so finally got a chance to test null vs undefined more. I'm not sure why this works for you, but even with a brand new project it only omitted the type when I used null

I took the following steps:

  1. Create a new vite app (npm create vite@latest story-demo -- --template react-ts)
  2. Run npx storybook@latest init
  3. Tried to edit the generated Button.stories.ts to remove the default type for the size prop.

undefined did not work

It still showed union as the type.

const meta = {
  ...
  argTypes: {
    ...
    size: {
      table: { type: { summary: undefined } }
    }
  },
 ...
} satisfies Meta<typeof Button>;
Summary undefined

null worked

Though of course it produces a typescript error:

    ...
    size: {
      table: { type: { summary: null } }
    }
    ...
Summary null

So I believe we the updated type should be:

type?: { summary?: string | null; detail?: string };

dcneiner avatar Jun 25 '24 01:06 dcneiner

😲 ok. I'm fine with either undefined or null to be honest

Hyzual avatar Jun 25 '24 14:06 Hyzual