TypeScript Config
This is a central reference for using Storybook with TypeScript.
Typescript configuration presets
The fastest and easiest way to write and configure your stories in TypeScript is by using a Storybook preset.
-
If you’re using Create React App (CRA) and have configured it to work with TS, you should use
@storybook/preset-create-react-app
, which configures Storybook to reuse CRA’s TS handling. -
If you are not using CRA or have other requirements, then the next best option is
@storybook/preset-typescript
, which configurests-loader
under the hood.
If you need more control than these presets offer, read on for manual configuration instructions.
You can learn more about Storybook presets over here.
If using TypeScript, some addons require features available in TS version 3.4+.
Setting up TypeScript with ts-loader
Dependencies you may need
Setting up TypeScript to work with Storybook
We configure storybook’s webpack by changing .storybook/main.js
:
The above example shows a working Webpack config with the TSDocgen plugin integrated. This plugin is not necessary to use Storybook and the section marked // optional
can be safely removed if the features of TSDocgen are not required.
tsconfig.json
This is for the default configuration where /stories
is a peer of src
. If you have them all in src
, you may wish to replace "rootDirs": ["src", "stories"]
above with "rootDir": "src",
.
Setting up TypeScript with babel-loader
A note for Create React App users
Please use @storybook/preset-create-react-app
for full compatibility with Create React App features - which includes TypeScript support.
Setting up TypeScript to work with Storybook
The following code uses babel-preset-react-app
.
We will create a custom Webpack config by creating editing/creating the .storybook/main.js
:
tsconfig.json
If your stories are outside the src
folder, for example the stories
folder in root, then "rootDirs": ["src", "stories"]
needs to be added to be added to compilerOptions
so it knows what folders to compile. Make sure jsx
is set to preserve. Should be unchanged.
Create a TSX storybook index
The default storybook index file is stories/index.stories.js
— you’ll want to rename this to stories/index.stories.tsx
.
Using TypeScript with the TSDocgen addon
The very handy Storybook Info addon autogenerates prop tables documentation for each component, however it doesn’t work with Typescript types. The current solution is to use react-docgen-typescript-loader to preprocess the TypeScript files to give the Info addon what it needs. The webpack config above does this, and so for the rest of your stories you use it as per normal:
Customizing Type annotations/descriptions
Please refer to the react-docgen-typescript-loader docs for writing prop descriptions and other annotations to your Typescript interfaces.
Additional annotation can be achieved by setting a default set of info parameters in .storybook/preview.js
:
Note: Component docgen information can not be generated for components that are only exported as default. You can work around the issue by exporting the component using a named export.
Setting up Jest tests
The ts-jest README explains pretty clearly how to get Jest to recognize TypeScript code.
This is an example jest.config.js
file for jest:
Building your TypeScript Storybook
You will need to set up some scripts - these may help: