question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook controls TypeScript inference not working as expected in a NX workspace

See original GitHub issue

Describe the bug When a component refers to a type from another lib in a NX workspace via the alias created by NX storybook controls fail to infer the type correctly.

To Reproduce This reproduction was setup via NX CLI commands since the behavior is exhibited that way.

Repo: https://github.com/rmevans9/storybook-ts-issue Storybook: https://main--61f9e6cee45291003a1e5539.chromatic.com/

System Please paste the results of npx sb@next info here.

Environment Info:

  System:
    OS: macOS 11.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Safari: 15.0
  npmPackages:
    @storybook/addon-essentials: ~6.4.12 => 6.4.17
    @storybook/builder-webpack5: ~6.4.12 => 6.4.17
    @storybook/manager-webpack5: ~6.4.12 => 6.4.17
    @storybook/react: ~6.4.12 => 6.4.17

Additional context I suspect it has something to do with just the alias setup that NX uses because as you see in the reproduction case (https://github.com/rmevans9/storybook-ts-issue/blob/main/libs/storybook/src/lib/TestComponent.tsx#L2-L13) I import the exact same type from the same file and it has different outcomes. The only difference is direct path import vs the alias import.

Also note that all of the properties defined have effectively the same type as far as TypeScript is concerned Screen Shot 2022-02-01 at 9 21 40 PM

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
rmevans9commented, Feb 6, 2022

After digging through a bunch of code to try and locate the issue following the logic through all kinds of places I finally have the actual root cause for this issue.

To start with: This is 100% an issue because of the NX configuration. That said I likely need to move this issue over to https://github.com/nrwl/nx

That said I will put my findings here to help anyone that may come to this repo in search of the solution to this problem in the future. What is occuring is react-docgen-typescript defaults to looking for tsconfig.json in the root of the project folder. This would be fine if the context was within the lib folder that storybook is running for but for some reason, likely because of the setup where there is a main.js in a .storybook folder at the root of the nx workspace, the root of where it is looking is the root of the workspace. In a default configured nx workspace there is no tsconfig.json at that level. Only a tsconfig.base.json that gets imported by all apps / libs. Therefore the issue existed because react-docgen-typescript was unable to find the tsconfig.json that told it the paths that get used by nx to alias libs. The fix is really simple once that is all understood. In the main.ts of the .storybook folder add the following:

  typescript: {
    reactDocgenTypescriptOptions: {
      tsconfigPath: './tsconfig.base.json',
    },
  },
2reactions
shilmancommented, Feb 8, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

@storybook/addon-controls | Yarn - Package Manager
Storybook Controls gives you a graphical UI to interact with a component's arguments dynamically, without needing to code. It creates an addon panel...
Read more >
Faster Builds with Module Federation - Nx
Module Federation provides a solution to the scaling problem by allowing a Single Page Application (SPA) to be sliced into multiple smaller remote...
Read more >
Why nx is throwing error when running storybook
I'm trying to run my custom react component library through storybook with nx. Expected Result. Whenever I run nx run my-lib:storybook ...
Read more >
Creating a React component library using Storybook 6
Learn how to build a React component library using Storybook 6 and TypeScript, compile it with Rollup and publish it.
Read more >
VS Code Jest, Improved ESLint Support, Typescript ... - Nx Blog
VS Code Jest, Improved ESLint Support, Typescript 4, Storybook 6, and more with Nx 10.3. Today, we are happy to announce the release...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found