不能将 props 用于带有 emotion-js 和 typescript 的样式化组件

Can't use props for styled components with emotion-js and typescript

这是我非常简单的组件:

import React, { ReactChild, ElementType } from 'react'
import styled from '@emotion/styled'

type WrapperPropsType = {
  size?: SizeType
}

type ButtonPropsType = {
  as?: ElementType<any>,
  size?: SizeType,
  children?: ReactChild
}

const Wrapper = styled.button<WrapperPropsType>((props: WrapperPropsType) => ({
  fontSize: FontSize[props.size],
}))

const Button = ({ as, size, children }: ButtonPropsType) => {
  return (
    <Wrapper as={as} size={size}>
        { children }
    </Wrapper>
  )
}

我的.tsconfig:

{
  "compilerOptions": {
    "types": ["node", "@emotion/react"],
    "declaration": true,
    "declarationDir": "build",
    "module": "esnext",
    "target": "es5",
    "sourceMap": true,
    "jsx": "preserve",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"],
  "exclude": [
    "node_modules",
    "build",
    "src/**/*.test.tsx"
  ]
}

但是在编译时出现下一个错误:

Error: src/components/Button/Button.tsx(18,30): semantic error TS2339: Property 'size' does not exist on type '{ theme?: Theme; as?: ElementType; } & Pick<DetailedHTMLProps<ButtonHTMLAttributes, HTMLButtonElement>, "form" | ... 265 more ... | "onTransitionEndCapture"> & { ...; }'.

我做错了什么?感谢您的帮助

软件包版本: @emotion/react版本:11.1.2 @emotion/styled版本:11.0.0 typescript版本:4.1.3

您在 WrapperPropsType 中定义了 size 但没有使用它。添加它,它将起作用

const Button = ({ as, size, children }: ButtonPropsType & WrapperPropsType) => {

我通过将 rollup-plugin-typescript2 替换为 @rollup/plugin-typescript 设法解决了这个问题。代码没有错误。