繁体   English   中英

样式化组件的“css”道具和 Storybook 的 TypeScript 问题

[英]TypeScript issue with styled-component's "css" prop and Storybook

从 Storybook 导入内容后,我在 TypeScript 中启用 styled-component 的css prop 时遇到了问题,因为 Storybook 依赖于@emotion/core ,其类型声明定义了 Emotion 自己的css prop。 该错误源于两个css prop 类型不兼容的事实。

我熟悉在 TypeScript 中启用css prop 的推荐方法(包括Stack Overflow 上的答案),以及关于如何处理上述 Storybook 问题的建议。

这是应用程序代码index.tsx

import React from 'react'
import { css } from 'styled-components'

export default function App() {
  return (
    <h1
      css={css`
        color: red;
      `}
    >
      Hello world!
    </h1>
  )
}

这是类型声明styled-components.d.ts ,其中包含尝试的修复:

import { CSSProp } from "styled-components";

// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31245#issuecomment-780019806

declare module 'react' {
  interface DOMAttributes<T> {
    css?: CSSProp
  }
}
declare global {
  namespace JSX {
    interface IntrinsicAttributes {
      css?: CSSProp
    }
  }
}

这一直有效,直到我从导入@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.

这是从此存储库创建的 CodeSandbox ,它重现了这个问题。

根据styled-components库的文档,您似乎应该导入style而不是css

我创建了一个示例来展示如何执行此操作:
https://codesandbox.io/s/react-typescript-forked-1xkww4?file=/src/App.tsx

或者,您可以尝试将css转换为其他内容,尽管我不确定它是否有助于解决您遇到的类型定义冲突。

import { css as StyledCss } from 'styled-components';

希望这会有所帮助。

这似乎是由于混合输入情感和样式组件引起的问题。 请参阅此链接https://stackoverflow.com/a/52129505/4449475

我解决了。 这个问题的解决方案是我们需要覆盖 h1、h2、div 等内置组件的“css”道具类型,我们可以通过在根文件夹中创建名为“cssprop.d.ts”的新文件来覆盖它. 将以下代码添加到“cssprop.d.ts”中

import { CSSProp } from "styled-components";

// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31245#issuecomment-780019806

declare module "react" {
  interface DOMAttributes<T> {
    css?: CSSProp;
  }
}
declare global {
  namespace JSX {
    interface IntrinsicAttributes {
      css?: CSSProp;
    }
  }
}

请访问以下链接以检查解决方案。 https://codesandbox.io/s/stupefied-brahmagupta-8lngz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM