Javascript 带有样式化组件的TypeScript问题';s";css";道具和故事书
当我从Storybook导入一些东西时,我在TypeScript中启用样式化组件的Javascript 带有样式化组件的TypeScript问题';s";css";道具和故事书,javascript,reactjs,typescript,styled-components,Javascript,Reactjs,Typescript,Styled Components,当我从Storybook导入一些东西时,我在TypeScript中启用样式化组件的css道具时遇到了问题,因为Storybook依赖于@emotion/core,其类型声明定义了emotion自己的css道具。错误源于两种cssprop类型不兼容的事实 我熟悉在TypeScript(包括)中启用cssprop的方法,以及对上面解释的故事书问题的建议 这是应用程序代码,index.tsx: import React from 'react' import { css } from 'styled-
css
道具时遇到了问题,因为Storybook依赖于@emotion/core
,其类型声明定义了emotion自己的css
道具。错误源于两种css
prop类型不兼容的事实
我熟悉在TypeScript(包括)中启用css
prop的方法,以及对上面解释的故事书问题的建议
这是应用程序代码,index.tsx
:
import React from 'react'
import { css } from 'styled-components'
export default function App() {
return (
<h1
css={css`
color: red;
`}
>
Hello world!
</h1>
)
}
直到我从导入@emotion/core
的Storybook中导入index.tsx
中的某些内容时,这种方法才起作用,例如:
import { storiesOf } from '@storybook/react'
// ...
然后TypeScript失败,出现以下错误:
index.tsx:8:7 - error TS2322: Type 'FlattenSimpleInterpolation' is not assignable to type 'InterpolationWithTheme<any>'.
Type 'readonly SimpleInterpolation[]' is not assignable to type 'ObjectInterpolation<undefined>'.
Types of property 'filter' are incompatible.
Type '{ <S extends SimpleInterpolation>(predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => value is S, thisArg?: any): S[]; (predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => unknown, thisArg?: any): SimpleInterpolation[]; }' is not assignable to type 'string | string[] | undefined'.
Type '{ <S extends SimpleInterpolation>(predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => value is S, thisArg?: any): S[]; (predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => unknown, thisArg?: any): SimpleInterpolation[]; }' is missing the following properties from type 'string[]': pop, push, concat, join, and 27 more.
8 css={css`
~~~
node_modules/@emotion/core/types/index.d.ts:84:5
84 css?: InterpolationWithTheme<any>
~~~
The expected type comes from property 'css' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'
styled-components.d.ts:7:5 - error TS2717: Subsequent property declarations must have the same type. Property 'css' must be of type 'InterpolationWithTheme<any>', but here has type 'CSSProp<any> | undefined'.
7 css?: CSSProp
~~~
node_modules/@emotion/core/types/index.d.ts:84:5
84 css?: InterpolationWithTheme<any>
~~~
'css' was also declared here.
styled-components.d.ts:13:7 - error TS2717: Subsequent property declarations must have the same type. Property 'css' must be of type 'InterpolationWithTheme<any>', but here has type 'CSSProp<any> | undefined'.
13 css?: CSSProp
~~~
node_modules/@emotion/core/types/index.d.ts:96:7
96 css?: InterpolationWithTheme<any>
~~~
'css' was also declared here.
index.tsx:8:7-错误TS2322:类型“FlattsSimpleInterpolation”不可分配给类型“InterpolationWithTheme created”,从中复制此问题。我建议将可复制的示例克隆到@MikeS。这是因为我不熟悉Stackblitz,我希望这能有所帮助
index.tsx:8:7 - error TS2322: Type 'FlattenSimpleInterpolation' is not assignable to type 'InterpolationWithTheme<any>'.
Type 'readonly SimpleInterpolation[]' is not assignable to type 'ObjectInterpolation<undefined>'.
Types of property 'filter' are incompatible.
Type '{ <S extends SimpleInterpolation>(predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => value is S, thisArg?: any): S[]; (predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => unknown, thisArg?: any): SimpleInterpolation[]; }' is not assignable to type 'string | string[] | undefined'.
Type '{ <S extends SimpleInterpolation>(predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => value is S, thisArg?: any): S[]; (predicate: (value: SimpleInterpolation, index: number, array: readonly SimpleInterpolation[]) => unknown, thisArg?: any): SimpleInterpolation[]; }' is missing the following properties from type 'string[]': pop, push, concat, join, and 27 more.
8 css={css`
~~~
node_modules/@emotion/core/types/index.d.ts:84:5
84 css?: InterpolationWithTheme<any>
~~~
The expected type comes from property 'css' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'
styled-components.d.ts:7:5 - error TS2717: Subsequent property declarations must have the same type. Property 'css' must be of type 'InterpolationWithTheme<any>', but here has type 'CSSProp<any> | undefined'.
7 css?: CSSProp
~~~
node_modules/@emotion/core/types/index.d.ts:84:5
84 css?: InterpolationWithTheme<any>
~~~
'css' was also declared here.
styled-components.d.ts:13:7 - error TS2717: Subsequent property declarations must have the same type. Property 'css' must be of type 'InterpolationWithTheme<any>', but here has type 'CSSProp<any> | undefined'.
13 css?: CSSProp
~~~
node_modules/@emotion/core/types/index.d.ts:96:7
96 css?: InterpolationWithTheme<any>
~~~
'css' was also declared here.