storybook: [Bug]: `decorators` causes TS error
Describe the bug
Following the code from the docs, I get a TS error:
TS2742: The inferred type of
metacannot be named without a reference to.pnpm/@storybook+types@7.5.2/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.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 13
- Comments: 19 (9 by maintainers)
I have the same issue, workarounds didn’t work for me. Adding type
Meta<...>instead ofsatisfiesworkedHad 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:
It seems refactoring to drop
satisfiesoperator solves the problem.I can only reproduce this when setting
in tsconfig
@emlai Are you trying to build TS declaration files for
stories.tsfiles? 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.jsonfor building declerations than your regulartsconfig.jsonfile that is used by your IDE or for typechecking in CI.I have the same issue.
The following seems to be working for me:
i.e. just add a type definition to
Story. You can also write:(I’m still on storybook v7, not checked v8 yet.)
The issue for me was: Forgot that the story extension as
.ts, changed it to*.tsxand now it is ok.@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.tsxfrom emittingd.ts, but still keep it being typechecked.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.