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

[Bug]: decorators causes TS error #24656

Open
emlai opened this issue Nov 1, 2023 · 16 comments
Open

[Bug]: decorators causes TS error #24656

emlai opened this issue Nov 1, 2023 · 16 comments

Comments

@emlai
Copy link

emlai commented Nov 1, 2023

Describe the bug

Following the code from the docs, I get a TS error:

TS2742: The inferred type of meta cannot be named without a reference to .pnpm/@[email protected]/node_modules/@storybook/types. This is likely not portable. A type annotation is necessary.

To Reproduce

import type { Meta } from "@storybook/react";

const meta = {
  component: Icon,
  decorators: [
    Story => (
      <div style={{ margin: '3em' }}>
        <Story />
      </div>
    ),
  ],
} satisfies Meta<typeof Icon>;

export default meta;

If I remove decorators, the error goes away.

System

System:
    OS: macOS 13.5
    CPU: (12) arm64 Apple M2 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.8.1 - /opt/homebrew/bin/node
    npm: 10.1.0 - /opt/homebrew/bin/npm
    pnpm: 8.6.8 - /opt/homebrew/bin/pnpm <----- active
  Browsers:
    Chrome: 118.0.5993.117
    Safari: 16.6

Additional context

TypeScript version is 5.2.2.

Adding import type {} from "@storybook/types" is a workaround but ideally it would not be required.

@shilman
Copy link
Member

shilman commented Nov 3, 2023

Do you a have a reproduction repo you can share? If not, can you create one? Go to https://storybook.new or see repro docs. Thank you! 🙏

@vanessayuenn vanessayuenn moved this to Ready for work in Core Team Projects Nov 6, 2023
@vanessayuenn vanessayuenn moved this from Ready for work to Empathy Backlog in Core Team Projects Nov 13, 2023
@sirtobey
Copy link

I have the same issue.

@peterctct
Copy link

Also having the same issue on v7.6.2. This is on a fresh project.

Unfortunately, @emlai's workaround of using import type {} from "@storybook/types" does not seem to quiet the TS error in our case.

@Chris112
Copy link

After installing @storybook/types as a dev dependency and adding the import to a types.ts file in the root (anywhere in the project out of sight should work) the error went away for me.

@YouHusam
Copy link

I have the same issue, workarounds didn't work for me.
Adding type Meta<...> instead of satisfies worked

import type { Meta } from "@storybook/react";

const meta: Meta<typeof Icon> = {
  component: Icon,
  decorators: [
    Story => (
      <div style={{ margin: '3em' }}>
        <Story />
      </div>
    ),
  ],
};

export default meta;

@akifunal
Copy link

akifunal commented Jan 5, 2024

I have the same issue 🙏🏻

@vanessayuenn vanessayuenn added this to the 8.0-beta milestone Jan 9, 2024
@vanessayuenn vanessayuenn modified the milestones: 8.0-beta, 8.0-RC Jan 9, 2024
@kasperpeulen
Copy link
Contributor

I can not reproduce this, can you check if this error still happens in the latest storybook, and maybe a stackblitz repro? @emlai

@kasperpeulen kasperpeulen self-assigned this Jan 12, 2024
@generatorpoint
Copy link

I have the same issue @YouHusam 's workaround fixed it for me

@kasperpeulen
Copy link
Contributor

@generatorpoint Could you make a stackblitz reproduction. I can not reproduce this issue.

@vanessayuenn vanessayuenn moved this from Empathy Backlog to Waiting in Core Team Projects Feb 12, 2024
@vanessayuenn vanessayuenn modified the milestones: 8.0-RC, 8.0-Stable Feb 12, 2024
@valentinpalkovic
Copy link
Contributor

It is easy to reproduce it in a pnpm environment because the @storybook/types package is not installed in the root directory of node_modules and therefore, TypeScript complains.

@shilman
Copy link
Member

shilman commented Feb 21, 2024

@valentinpalkovic is that because there is some type leaking from @storybook/types? AFAIK the user shouldn't ever need to reference that and should get all of their types through the appropriate renderer (e.g. @storybook/react, @storybook/vue3, etc.).

@valentinpalkovic
Copy link
Contributor

valentinpalkovic commented Feb 21, 2024

So the satisfies Meta<...> annotation of the meta variable tells TypeScript to still infer the proper type of meta. When it tries to infer the type of the StoryFn parameter inside the decorator, it infers it as PartialStoryFn, which is NOT exported from @storybook/react, which I think is the root cause of the issue. There might be still other types, which are available on @storybook/types and which are not re-exported by @storybook/react.

@vanessayuenn vanessayuenn modified the milestones: 8.0-Stable, 8.0-RC Feb 21, 2024
@kasperpeulen
Copy link
Contributor

kasperpeulen commented Feb 26, 2024

I can only reproduce this when setting

  "declaration": true,
  "noEmit": false,

in tsconfig

@emlai Are you trying to build TS declaration files for stories.ts files?
I think ideally stories should be excluded when building d.ts files for a library, but maybe I'm overseeing a usecase.

You can achieve this by using a different tsconfig.build.json for building declerations than your regular tsconfig.json file that is used by your IDE or for typechecking in CI.

@vanessayuenn vanessayuenn moved this from Waiting to Empathy - Ready for work in Core Team Projects Feb 26, 2024
@kasperpeulen
Copy link
Contributor

@kylegach @vanessayuenn Discussed with @shilman that this is not a blocker for RC.

I think this issue is best solved by instructing people how to exclude *.stories.tsx from emitting d.ts, but still keep it being typechecked.

@kasperpeulen kasperpeulen removed this from the 8.0-RC milestone Feb 26, 2024
@kasperpeulen kasperpeulen removed their assignment Feb 27, 2024
@vanessayuenn vanessayuenn added this to the 8.0-Stable milestone Feb 28, 2024
@vanessayuenn
Copy link
Contributor

potentially related to #25170

@vanessayuenn vanessayuenn moved this from Empathy - Ready for work to Empathy Backlog in Core Team Projects Apr 9, 2024
@AgataJedryszek-da
Copy link

Had the same issue in Storybook v8.3.6. I solved this by rewriting the code so that it's structured the same way as the example in Storybook docs. Without testing the code provided in this issue, it would be:

const meta: Meta<typeof Icon> = {
  component: Icon,
  decorators: [
    Story => (
      <div style={{ margin: '3em' }}>
        <Story />
      </div>
    ),
  ],
};

export default meta;

It seems refactoring to drop satisfies operator solves the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Empathy Backlog
Status: No status
Status: No status
Development

Successfully merging a pull request may close this issue.