presets
presets copied to clipboard
preset-typescript does not work according to instructions
I have a clean setup for storybook/react: npx -p @storybook/cli sb init --type react
I added preset-typescript to new presets.js file like that module.exports = ['@storybook/preset-typescript'];
and dependencies npm i -D @storybook/preset-typescript react-docgen-typescript-loader ts-loader
Now I have quite a simple basic configuration:
/*.storybook/config.js*/
import { configure } from '@storybook/react';
// automatically import all files ending in *.stories.js
configure(require.context('../stories', true, /\.stories\.ts$/), module);
/*.storybook/presets.js*/
module.exports = ['@storybook/preset-typescript'];
/*.storybook/addons.js*/
import '@storybook/addon-actions/register';
import '@storybook/addon-links/register';
/*stories/0-Welcome.stories.ts*/
import React from 'react';
import { linkTo } from '@storybook/addon-links';
import { Welcome } from '@storybook/react/demo';
export default {
title: 'Welcome',
};
export const toStorybook = () => <Welcome showApp={linkTo('Button')} />;
toStorybook.story = {
name: 'to Storybook',
};
And instruction says that's it, but:
So how does it work?
@rpolonsky what happens when you name that 0-Welcome.stories.tsx
?
I'm running into the same problem. Changing the extensions from .ts
to .tsx
did not solve the issue.
I was trying to create the smallest reproduction inside of a Next.js app. I think the issue stems from an incompatible tsconfig.json
. And since the documentation doesn't hint at the necessary configuration options, Storybook fails to transpile the Typescript code.
For reference, here is my tsconfig.json
that works:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react"
},
"include": ["src", "stories"]
}
A few notes:
-
"jsx"
is set to"react"
and not"preserve"
- Although I am trying to setup within a Next.js app, I stole this config file from create-react-app's
tsconfig.json
- I changed the value of
"noEmit"
fromtrue
tofalse
- I appended
"stories"
to the"include"
array - ~For the demo to work, I needed to supply custom type definitions for the
'@storybook/react/demo'
components - as TS complained. However, this is likely due to the"strict"
option beingtrue
~ It was caused by the"strict"
option.
@adam-beck Thanks for the thorough diagnosis! 🙌 How would you recommend improving the docs for your use case?
For starters, it might be best to ensure that the tsconfig.json
file that comes with the preset works (without error) for a clean (i.e. npx -p @storybook/cli sb init
) Storybook setup. Since it uses the rule "noImplicitAny": true,
, it fails to compile since there are no typings for the demo components. Either the typings should be generated or the rule should be toggled to false
. I think the former would be the ideal solution but the latter would be much easier.
The documentation could also mention which configuration options are absolutely necessary such as "jsx": "react"
. The smallest tsconfig.json
I could create and still get it to work is:
{
"compilerOptions": {
"module": "es6",
"jsx": "react",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
}
It may be worthwhile to mention that those need to be set as is.
Other than that, though, it's hard because each project has a unique tsconfig.json
. It would be impractical to include even a fraction of the scenarios. I would suggest advising that
- any existing
tsconfig.json
will likely need to be overridden & include an example of doing so (it sort of already does this) - explain that if the application already has a
.storybook/config.js
it will need to be changed to search for.tsx?
files
Edit: It looks like the demo components have typings already! I'm not sure why they aren't being picked up though. After searching, it looks like this has already been fixed too!
Hi have the same issue, however the tsconfig file did not work for me. We keep our stories in folders in with the component, would this make a difference? Below is a the error, looks like nothing is being transpiled using this preset.
I'm having the same issue, tried from scratch a few times, but having the same issue that @daraclare has.
Bump. Me three.
tsconfig:
It looks to me like the problem is with the underlying webpack config. Running npx start-storybook --debug-webpack
when presets.js only has module.exports = ["@storybook/preset-typescript"];
produces the following:
// ...
{
test: /\.tsx?$/,
use: [
{
loader: '/Users/joe/Projects/test/node_modules/ts-loader/index.js',
options: { transpileOnly: true }
},
{
loader: '/Users/joe/Projects/test/node_modules/react-docgen-typescript-loader/dist/index.js',
options: {}
}
],
include: []
},
// ...
Notice the empty include
array. But, if you change presets.js such that include
is specified...
const path = require("path");
module.exports = [
{
name: "@storybook/preset-typescript",
options: {
include: [
path.resolve(__dirname, "../src"),
path.resolve(__dirname, "../stories")
]
}
}
];
... everything works as expected and the relevant portion of the webpack config has the correct include directive:
// ...
{
test: /\.tsx?$/,
use: [
{
loader: '/Users/joe/Projects/test/node_modules/ts-loader/index.js',
options: { transpileOnly: true }
},
{
loader: '/Users/joe/Projects/test/node_modules/react-docgen-typescript-loader/dist/index.js',
options: {}
}
],
include: [
'/Users/joe/Projects/test/src',
'/Users/joe/Projects/test/stories'
]
},
// ...
Just for the sake of comparison, I checked the generated webpack config in a CRA project and found that it's using this:
// ...
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [
'/Users/joe/Projects/cra-test/src',
'/Users/joe/Projects/cra-test/.storybook'
],
// ...
I'm not sure why the include
array is empty when the directions for installing this preset are followed as published, but I'm reasonably sure that's the issue.
Looks like include
defaults to an empty array here: https://github.com/storybookjs/presets/blob/master/packages/preset-typescript/index.js#L9
If we can take a note from CRA, maybe it could default to something more like this:
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
...
include = [
resolveApp('src'),
resolveApp('stories'),
resolveApp('.storybook')
]
I was gonna create a fork and throw up a pull request for this, but it looks like that's already being addressed by @T0MASD in #83 👍
@wollardj I struggled trying to get this to work for a long time before I found this issue. Setting options.include
on the @storybook/preset-typescript
plugin did the trick. Thank you!
Looks like
include
defaults to an empty array here: https://github.com/storybookjs/presets/blob/master/packages/preset-typescript/index.js#L9If we can take a note from CRA, maybe it could default to something more like this:
const appDirectory = fs.realpathSync(process.cwd()); const resolveApp = relativePath => path.resolve(appDirectory, relativePath); ... include = [ resolveApp('src'), resolveApp('stories'), resolveApp('.storybook') ]
Good catch! I had been frustrated with this for a while, but setting these in options.include
on the preset, as @kevinbarabash said, fixed it for me. Thanks!
Trying to integrate Storybook as a part of a TSDX component library.
Steps I took:
-
npx -p @storybook/cli sb init --type react
-
yarn add -D @storybook/preset-typescript react-docgen-typescript-loader ts-loader fork-ts-checker-webpack-plugin
-
Convert
.storybook/main.js
to:
module.exports = {
stories: ['../src/components/**/*.stories.(tsx|mdx)'],
addons: ['@storybook/addon-actions', '@storybook/addon-links'],
presets: ['@storybook/preset-typescript'],
};
- Created the following
Button.stories.tsx
withinsrc/components/Button/__stories__/
:
import React from 'react';
import { action } from '@storybook/addon-actions';
import { Button } from '../';
export default {
title: 'Button',
component: Button,
};
export const WithRequiredProps = () => (
<Button onClick={action('clicked')}>Click me!</Button>
);
After I run, yarn storybook
, I get the following:
I ditched the preset and got everything working with this:
-
npx -p @storybook/cli sb init --type react
-
yarn add -D yarn add -D awesome-typescript-loader @storybook/addon-info react-docgen-typescript-loader
-
.storybook/main.js
:
module.exports = {
stories: ['../src/**/*.stories.(tsx|mdx)'],
addons: ['@storybook/addon-actions', '@storybook/addon-links'],
webpackFinal: async (config) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('awesome-typescript-loader'),
},
{
loader: require.resolve('react-docgen-typescript-loader'),
},
],
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
},
};
I spend two work day for solve this issue. Then finally i solved it. Here is my solution step by step:
First, i follow the for installing storybook Then i look up for typescript/cra support. doc They are recommend @storybook/preset-create-react-app to use preset for CRA (also support typescript/cra). There is no much config, the project directly started BUT when i change the file extension (.js to .tsx) i faced that error.
- I install the ts-loader Setting up TypeScript with ts-loader
- Edit
storybook/main.js
Setting up TypeScript with babel-loader
module.exports = {
stories: ['../src/components/**/*.stories.tsx'],
webpackFinal: config => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [['react-app', { flow: false, typescript: true }]],
}
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
},
};
stories: ['../src/components/**/*.stories.tsx'],
Note: Stories path can change. Each story placed inside the component folder
Then Install following packages:
npm install --save-dev babel-loader babel-preset-react-app
- Start App
npm run storybook
Then i faced new error:
Problem is resolve path alias. You can solve this error like that:
import Text from 'components/text';
to import Text from '../../text';
But i don't want to do that. That's why i add that config to .storybook/main.js
:
resolve: {
alias: {
components: path.resolve(__dirname, '../src/components'),
styles: path.resolve(__dirname, '../src/styles'),
}
}
Then Boom:
Note: There is no tricky things. I just follow the manual installation on storybook. By the way, thanks for this beautiful project.
This should be improved when v6 is released 🙏
@kylemh was it? I'm trying to get everything working but things blow up when I add @storybook/preset-typescript
in v6, typescript support is internal
you're not meant to use the preset
https://storybook.js.org/docs/react/configure/typescript