TypeScript
Storybook provides an integrated TypeScript experience, including zero-configuration setup and built-in types for APIs, addons, and stories.
Storybook's configuration file (i.e.,
main.ts
) is defined as an ESM module written in TypeScript, providing you with the baseline configuration to support your existing framework while enabling you stricter type-checking and autocompletion in your editor. Below is an abridged configuration file.
See the main configuration API reference for more details and additional properties.
See the Vite builder
TypeScript documentation
if using
@storybook/builder-vite
.
Out of the box, Storybook is built to work with a wide range of third-party libraries, enabling you to safely access and document metadata (e.g., props) for your components without any additional configuration. It relies on
react-docgen
, a fast and highly customizable parser to process TypeScript files to infer the component's metadata and generate types automatically for improved performance and type safety. If you need to customize the default configuration for a specific use case scenario, you can adjust your Storybook configuration file and provide the required options. Listed below are the available options and examples of how to use them.
Option | Description |
---|---|
check
|
Available for Webpack-based projects.
Enables type checking within Storybook
typescript: { check: true },
|
checkOptions
|
Requires the
check
option to be enabled.
Configures the
fork-ts-checker-webpack-plugin
plugin
typescript: { checkOptions: {},},
|
reactDocgen
|
Configures the TypeScript parser used by Storybook.
Available options:
react-docgen
(default),
react-docgen-typescript
,
false
typescript: { reactDocgen: 'react-docgen'},
|
reactDocgenTypescriptOptions
|
Requires the
reactDocgen
option to be
react-docgen-typescript
.
Configures the
react-docgen-typescript-plugin
plugin per builder
typescript: { reactDocgen: 'react-docgen-typescript', reactDocgenTypescriptOptions: {},},
|
skipCompiler
|
Disables parsing Typescript files through the compiler
typescript: { skipCompiler:false,},
|
Additional options are available for the
typescript
configuration option. See the
config.typescript
API reference
for more information.
Storybook provides zero-config TypeScript support, allowing you to write stories using this language without additional configuration. You can use this format for improved type safety and code completion. For example, if you're testing a
Button
component, you could do the following in your story file: