Automatic argTypes if component TypeScript prop types are imported #34477
Replies: 3 comments
|
Hi @martynhoyer! I'm Dosu and I’m helping the storybook team. This is a limitation of the default Switching to const config: StorybookConfig = {
// ...
typescript: {
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
shouldExtractLiteralValuesFromEnum: true,
shouldRemoveUndefinedFromOptional: true,
},
},
};The trade-off is speed — Give that a try and see if your To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
|
Thought the AI was on to something there, but unfortunately not. We're already using |
|
The dosubot answer is on the right track but incomplete. Two things to check: 1. Make sure your Storybook is pointed at the right tsconfig: // .storybook/main.ts
export default {
typescript: {
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
shouldExtractLiteralValuesFromEnum: true,
shouldRemoveUndefinedFromOptional: true,
// This is the key part — point to the tsconfig that includes your shared types
tsconfigPath: './tsconfig.json',
},
},
};2. Check how // ✅ Works — union type alias
export type Status = "inProgress" | "completed" | "deleted";
// ✅ Works — enum
export enum Status { InProgress = "inProgress", Completed = "completed", Deleted = "deleted" }
// ❌ Might not resolve — re-exported from another file
export type { Status } from "./other-file";If 3. Quick workaround if the above doesn't help — you can force the control type per-component without manually listing values: const meta = {
component: HeaderCard,
tags: ["autodocs"],
argTypes: {
status: {
control: 'select',
options: ["inProgress", "completed", "deleted"],
},
},
} satisfies Meta<typeof HeaderCard>;Not ideal since it duplicates the values, but at least it's at the meta level so all stories inherit it. What does the |
Uh oh!
There was an error while loading. Please reload this page.
Summary
Hi!
I've got some components which import their types from a shared types file because the same types are used throughout the codebase in different components. Here's a reduced example of a component with a
statusprop, which is limited to three potential options;"inProgress" | "completed" | "deleted"You can see in
HeaderCardProps, thestatusprop's value is imported from a shared file.When I create a Storybook story for that component and use Autodocs, I'd expect the props table to give me those three options as a
<select>or radio buttons, but instead, it gives me a string. This is what my story code looks like:And here's a screenshot of the props table in Storybook for that story:
If I remove the imported type and hard code the values instead, like this
interface HeaderCardProps { - status: Status; + status: "inProgress" | "deleted" | "completed"; }Then Storybook updates and gives me something more like what I'm after:
Although of course, the "description" column is missing the type name now.
Am I missing something, or is this a limitation of Storybook?
I'm assuming I can use the argTypes object to override it at a story level, but to me that seems to defeat the object of the autodocs feature, and gives me quite a lot more admin when creating Storybook stories.
Any assistance is greatly appreciated!
Additional information
I'm running the following versions (upgrading is possible, but organisationally challenging):
Storybook 9.1.1
React 16 🙄
Webpack 5
Create a reproduction
No response
All reactions