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.

importing types from @storybook/react breaks emotion css prop types

See original GitHub issue

Describe the bug For some odd reason, whenever types are imported from @storybook/react/types-6-0 or @storybook/react, the type definition for the css prop used in emotion will break and throw a Typescript error.

To Reproduce Steps to reproduce the behavior:

In the provided sandbox repo:

  1. Go to src/stories/Button.stories.tsx 2.Uncomment import from @storybook/react
  2. Open Button.tsx
  3. The following error will show up

Type ‘({ size }: any) => SerializedStyles’ is not assignable to type ‘Interpolation<undefined>’. Type ‘({ size }: any) => SerializedStyles’ is not assignable to type ‘ObjectInterpolation<undefined>’. Index signature is missing in type ‘({ size }: any) => SerializedStyles’. TS2322

  1. Comment out the import from @storybook/react
  2. The errors disappear.

Expected behavior the css prop should be able to take an array of functions that return SerializedStyles, without getting any Typescript error.

Screenshots If applicable, add screenshots to help explain your problem.

Code snippets Reproduce Repo

System Environment Info: System: OS: Linux 5.8 Pop!_OS 20.10 CPU: (4) x64 Intel® Core™ i5-4590 CPU @ 3.30GHz Binaries: Node: 14.15.0 - ~/.nvm/versions/node/v14.15.0/bin/node Yarn: 1.22.5 - /usr/bin/yarn npm: 6.14.8 - ~/.nvm/versions/node/v14.15.0/bin/npm Browsers: Chrome: 87.0.4280.66 Firefox: 84.0 npmPackages: @storybook/addon-actions: ^6.1.11 => 6.1.11 @storybook/addon-essentials: ^6.1.11 => 6.1.11 @storybook/addon-links: ^6.1.11 => 6.1.11 @storybook/node-logger: ^6.1.11 => 6.1.11 @storybook/preset-create-react-app: ^3.1.5 => 3.1.5 @storybook/react: ^6.1.11 => 6.1.11

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
disintegratorcommented, Jan 6, 2021

If your project depends on emotion 11 AND storybook, you’ll likely encounter a type conflict. This happens because the typescript compiler will, at a certain point, while compiling/type-checking your files, load @emotion/core as a transitive dependency via @storybook/theming, @storybook/addon-knobs, etc… (see yarn why @emotion/core). Now, @emotion/core declares a global type declaration for the css prop (see here). This declaration is incompatible when it encounters the css props in your codebase that rely on emotion 11’s definition of that same prop. To overcome this conflict in the css prop type declaration you can create a stub module in your codebase called emotion-core-stub.ts:

// emotion-core-stub.ts
export {}

and in tsconfig.json

{
  "compilerOptions": {
    // ... all your existing compiler options here ...

    "paths": {
      "@emotion/core": ["./emotion-core-stub.ts"]
    }
  }
}

This will prevent the conflict introduced by having emotion 10 and 11 in your project’s dependency tree.

12reactions
csantos1113commented, Aug 18, 2021

I don’t use @emotion in my codebase, but I see @emotion/core is a dependency of @storybook/ui an @storybook/theming which makes sense. But what does not make sense is to have css prop type being added to react interface (From here: https://github.com/emotion-js/emotion/blob/v10.0.1/packages/core/types/index.d.ts#L83-L99)

This is problematic when you use a different styled-library, because you’ll get a Types of property 'css' are incompatible.

Question: How can I ignore those types from my TS project?

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript issue with styled-component's "css" prop and ...
According to the documentation for the styled-components library, it seems that you should be importing style rather than css .
Read more >
How we migrated 541 components from Styled ... - Storybook
To keep all the codebases in sync, we don't use Emotion directly and instead export it via the @storybook/theming package: import { css,...
Read more >
The css Prop - Emotion
The primary way to style elements with emotion is the css prop. It provides a concise and flexible API to ... Import the...
Read more >
Storybook React: A Beginner's Tutorial to UI Components
@jsxImportSource @emotion/react */ import React from "react"; export default ... The PropTypes or TypeScript types are used by Storybook to ...
Read more >
react-storybook-addon-chapters | Yarn - Package Manager
React Storybook Chapters addon allows showcasing of multiple components within a story by breaking it down into smaller categories (chapters) and subcategories ...
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