Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stitches component "Types of property 'css' are incompatible." error occurred when storybook is installed #897

Closed
ainize-bot opened this issue Dec 14, 2021 · 17 comments

Comments

@ainize-bot
Copy link

ainize-bot commented Dec 14, 2021

Bug report

Describe the bug

SandBox Link

To Reproduce

I copied this Link to src/components/Button.tsx file.

and enter npx sb init command to terminal for install storybook. below packages are installed.

@babel/core, @storybook/addon-actions, @storybook/addon-essentials, @storybook/addon-links, @storybook/react

then, Button Component type error occurred.

for remove error, remove those packages from devDependencies.

Expected behavior

No Type Error.

Screenshots

스크린샷 2021-12-14 오후 8 39 59

System information

  • OS: macOS, sandbox
  • Version of Stitches: 1.2.6, 1.2.6
  • Version of Node.js: 14.18.2, ?
  • Version of Typescript: 4.4.2
  • Version of Next.js: 12.0.1
@ainize-bot ainize-bot changed the title typescript Button example have error "Types of property 'css' are incompatible." in next js typescript Button example have error "Types of property 'css' are incompatible." with storybook Dec 15, 2021
@ainize-bot ainize-bot changed the title typescript Button example have error "Types of property 'css' are incompatible." with storybook stitches component "Types of property 'css' are incompatible." error occurred when storybook is installed Dec 15, 2021
@iambrennanwalsh
Copy link

I've encountered the same exact issue. Goes away when uninstalling Storybook.

Here's all the relative code.

import type {ElementType, ReactNode} from 'react'
import type {Css, VariantProps, ComponentProps} from '@/theme'
import type {PolymorphicPropsWithoutRef} from 'react-polymorphic-types'
import {styled} from '@/theme'

// Types

export type Props<
  StitchesElement,
  Element extends ElementType = 'div'
> = PolymorphicPropsWithoutRef<
  {
    children?: ReactNode
    className?: string
    css?: Css
  } & VariantProps<StitchesElement> &
    ComponentProps<StitchesElement>,
  Element
>

export type Component<
  T,
  E extends ElementType = 'div',
  P extends Props<T, E> = Props<T, E>
> = (props: P) => JSX.Element

export const Container = styled('div', {
  margin: 'auto',
  padding: '$3',

  variants: {
    size: {
      '1': {maxWidth: '300px'},
      '2': {maxWidth: '585px'},
      '3': {maxWidth: '865px'}
    }
  }
})

export const PageContainer: Component<typeof Container> = props => {
  return (
    <Container as="div" size="1" {...props}>
      Hello World
    </Container>
  )
}

And here's the typescript errors im getting in VSCode.

No overload matches this call.
  Overload 1 of 3, '(props: Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; }, "size" | ... 1 more ... | "css"> & TransformProps<...> & { ...; }): ReactElement<...> | null', gave the following error.
    Type 'string' is not assignable to type 'undefined'.
  Overload 2 of 3, '(props: Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; }, "size" | ... 1 more ... | "css"> & TransformProps<...> & { ...; }): ReactElement<...> | null', gave the following error.
    Type '{ children: string; className?: string | undefined; css?: ({ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers...' is not assignable to type 'IntrinsicAttributes'.
      Types of property 'css' are incompatible.
        Type '({ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers-color-scheme: light)"; }, { ...; }, DefaultThemeMap, { .....' is not assignable to type 'InterpolationWithTheme<any>'.
          Type '{ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers-color-scheme: light)"; }, { ...; }, DefaultThemeMap, { ......' is not assignable to type 'InterpolationWithTheme<any>'.
            Type '{ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers-color-scheme: light)"; }, { ...; }, DefaultThemeMap, { ......' is not assignable to type 'Keyframes'.
              Type '{ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers-color-scheme: light)"; }, { ...; }, DefaultThemeMap, { ......' is missing the following properties from type '{ name: string; styles: string; anim: number; toString: () => string; }': name, styles, anim
  Overload 3 of 3, '(props: Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; }, "size" | "css"> & TransformProps<...> & { ...; }): ReactElement<...> | null', gave the following error.
    Type '{ children: string; className?: string | undefined; css?: ({ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers...' is not assignable to type 'IntrinsicAttributes'.
      Types of property 'css' are incompatible.
        Type '({ "@mobile"?: CSS<{ mobile: "(min-width: 32em)"; tablet: "(min-width: 48em)"; desktop: "(min-width: 64em)"; widescreen: "(min-width: 80em)"; highdef: "(min-width: 96em)"; ultrawide: "(min-width: 116em)"; dark: "(prefers-color-scheme: dark)"; light: "(prefers-color-scheme: light)"; }, { ...; }, DefaultThemeMap, { .....' is not assignable to type 'InterpolationWithTheme<any>'.

Also the following typescript error is popping up at: /node_modules/@stitches/react/types/styled-component.d.ts on line 27.

The expected type comes from property 'as' which is declared here on type 'IntrinsicAttributes & { css?: Interpolation<Theme>; } & Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<...>> & { ...; }, "size" | ... 1 more ... | "css"> & TransformProps<...> & { ...; }'

For your reference I will paste the contents of that file.

import type * as React from 'react'
import type * as Util from './util'


export type IntrinsicElementsKeys = keyof JSX.IntrinsicElements;

/** Returns a new Styled Component. */
export interface StyledComponent<
	Type = 'span',
	Props = {},
	Media = {},
	CSS = {}
> extends React.ForwardRefExoticComponent<
	Util.Assign<
		Type extends IntrinsicElementsKeys | React.ComponentType<any> 
			? React.ComponentPropsWithRef<Type>
		: never,
		TransformProps<Props, Media> & { css?: CSS }
	>
> {
	(
		props: Util.Assign<
			Type extends IntrinsicElementsKeys | React.ComponentType<any>
				? React.ComponentPropsWithRef<Type>
			: {},
			TransformProps<Props, Media> & {
				as?: never,
				css?: CSS
			}
		>
	): React.ReactElement | null

	<
		C extends CSS,
		As extends string | React.ComponentType<any> = Type extends string | React.ComponentType<any> ? Type : never
	>(
		props: Util.Assign<
			React.ComponentPropsWithRef<As extends IntrinsicElementsKeys | React.ComponentType<any> ? As : never>,
			TransformProps<Props, Media> & { 
				as?: As,
				css?: {
					[K in keyof C]: K extends keyof CSS ? CSS[K] : never
				}
			}
		>
	): React.ReactElement | null

	className: string
	selector: string

	[$$StyledComponentType]: Type
	[$$StyledComponentProps]: Props
	[$$StyledComponentMedia]: Media
}

/** Returns a new CSS Component. */
export interface CssComponent<
	Type = 'span',
	Props = {},
	Media = {},
	CSS = {}
> {
	(
		props?:
			& TransformProps<Props, Media>
			& {
				css?: CSS
			}
			& {
				[name in number | string]: any
			}
	): string & {
		className: string
		selector: string
		props: {}
	}

	className: string
	selector: string

	[$$StyledComponentType]: Type
	[$$StyledComponentProps]: Props
	[$$StyledComponentMedia]: Media
}

// ...

OS: macOS
Stitches Version: v1.2.6
Node Version: v16.13.1
Typescript Version: v4.4

@hwhmeikle
Copy link

hwhmeikle commented Dec 17, 2021

AFAIK Storybook has defined global type definitions for Emotion's css prop which clash with Stitches' css prop. A work around is to point the Emotion package at an empty types definition file. Here is a gist someone made of this: https://gist.github.com/sseagull/e2e7fe58f7f236897d342e776bb549a2

Perhaps this gist need to be referenced in an FAQ section of some sort as a lot of people encounter this.

@iambrennanwalsh
Copy link

Thanks! Here's what worked:

  1. Create a .d.ts file in the root of your project, or wherever convienant. I used types.d.ts.

  2. Add the following to your tsconfig.json.

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@emotion/core": ["./types.d.ts"]
    }
  }
}
  1. Restart your IDE.

For more detailed instructions, check out the gist hwhmeikle provided above.

@iambrennanwalsh
Copy link

If you use yarn, an even easier way to fix this would be adding a .yarnclean in the root of your repo with just the line @emotion/core/types.

@Tatametheus
Copy link

When adding x.d.ts in the project, do you guys get the error in the browser when doc page in storybook open?

Uncaught (in promise) TypeError: (0 , _emotion_core__WEBPACK_IMPORTED_MODULE_2__.keyframes) is not a function

@drianoaz
Copy link
Contributor

drianoaz commented Feb 8, 2022

Guys,

As of Storybook version 6.5.0-alpha.31, the solutions mentioned above no longer work.

This is because this PR changed the way the storybook generates typing definitions and the Emotion typing is now built into the Storybook theming package.

image

So, there were two ways to solve the problem:

// .yarnclean
@storybook/theming/dist/ts3.9/_modules/@emotion-core-types-index.d.ts
  • we could override the typing definition with an empty types definition file as mentioned by @hwhmeikle
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@storybook/theming": ["./types.d.ts"]
    }
  }
}

@ndelangen
Copy link

Is there any way to make the typings compatible?

@ndelangen
Copy link

@Andarist perhaps this is something you would have some advice on?

@Andarist
Copy link

@ndelangen the best fix here would be to upgrade Storybook to Emotion 11 because it no longer extends the global JSX namespace.

As a workaround, I think it would be possible to copy over Emotion 10 types to Storybook and remove those global extensions. That way your styled would still be roughly typed in the same way but you would "hide" references to Emotion types from TypeScript so it would never load them and thus it wouldn't create conflicts.

If you'd like my help with any of those - you know when to find me 😉

@csantos-nydig
Copy link

if this is helpful for anyone, I "fixed" this by patching @emotion/core using yarn-patch

diff --git a/types/index.d.ts b/types/index.d.ts
index 9395e0133f283f158913080c7dace4e79444e82c..d6ae72e0c47a197cd8e50ab93dff94fc05b69b22 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -78,22 +78,3 @@ export interface ClassNamesProps<Theme> {
 export function ClassNames<Theme = any>(
   props: ClassNamesProps<Theme>
 ): ReactElement
-
-declare module 'react' {
-  interface DOMAttributes<T> {
-    css?: InterpolationWithTheme<any>
-  }
-}
-
-declare global {
-  namespace JSX {
-    /**
-     * Do we need to modify `LibraryManagedAttributes` too,
-     * to make `className` props optional when `css` props is specified?
-     */
-
-    interface IntrinsicAttributes {
-      css?: InterpolationWithTheme<any>
-    }
-  }
-}
"resolutions": {
  "@emotion/core@^10.1.1": "patch:@emotion/[email protected]#.yarn/patches/@emotion-core-npm-10.1.1-f084e0eeac",
}

@drianoaz
Copy link
Contributor

if this is helpful for anyone, I "fixed" this by patching @emotion/core using yarn-patch

diff --git a/types/index.d.ts b/types/index.d.ts
index 9395e0133f283f158913080c7dace4e79444e82c..d6ae72e0c47a197cd8e50ab93dff94fc05b69b22 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -78,22 +78,3 @@ export interface ClassNamesProps<Theme> {
 export function ClassNames<Theme = any>(
   props: ClassNamesProps<Theme>
 ): ReactElement
-
-declare module 'react' {
-  interface DOMAttributes<T> {
-    css?: InterpolationWithTheme<any>
-  }
-}
-
-declare global {
-  namespace JSX {
-    /**
-     * Do we need to modify `LibraryManagedAttributes` too,
-     * to make `className` props optional when `css` props is specified?
-     */
-
-    interface IntrinsicAttributes {
-      css?: InterpolationWithTheme<any>
-    }
-  }
-}
"resolutions": {
  "@emotion/core@^10.1.1": "patch:@emotion/[email protected]#.yarn/patches/@emotion-core-npm-10.1.1-f084e0eeac",
}

@csantos-nydig is there any way to use this using yarn classic?

@csantos-nydig
Copy link

@drianoaz - I haven't use this in a while, but you'd need to use https://www.npmjs.com/package/patch-package I believe

@seedy
Copy link

seedy commented Feb 17, 2022

Confirming this mix of solutions from @drianoaz and @iambrennanwalsh solved the issue for me:

    "paths": {
      "@emotion/core": [
        "./types/index.d.ts"
      ],
      "@storybook/theming": [
        "./types/index.d.ts"
      ]
    }

I needed to ensure my file was existing and properly managed by my build config

@amehito
Copy link

amehito commented Mar 12, 2022

when:
stitches works, show error only after add storybook,
add types.d.ts, and modify tsconfig, set compilerOptions.paths,add
"@emotion/core": ["./types.d.ts"]
it works
thx

@ndelangen
Copy link

We're in the process of upgrading storybook to Emotion 11, and we have also removed the webpack alias, and removed the dependency on emotion from storybook in the storybook 6.4 alpha release.

@drianoaz
Copy link
Contributor

drianoaz commented Apr 7, 2022

Folks,

I updated my project to version 6.5.0-alpha.59 of the storybook and this problem was fixed

Can you see details here storybookjs/storybook#17640

Thanks @ndelangen

@hadihallak
Copy link
Member

Closing since this issue is not originating from stitches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests