【问题标题】:Property 'name' does not exist on type ThemedStyledPropsThemedStyledProps 类型上不存在属性“名称”
【发布时间】:2020-11-20 00:00:34
【问题描述】:

我收到一个错误

类型“ThemedStyledProps, “slot”上不存在属性“名称”| “风格” | ... 253 更多... | "css"> & { ...; }, 任何>'。

当使用代码sn-p时

// Styled Components
const IconContainer: any = styled.span.attrs(props => ({
  className: `icon-${ props.name }`
}))\`
  display: inline-block;
  font-size: ${ props => props.size };
  color: ${ props => props.color };
  transform: ${ props => props.transform};
\`;

在使用 props.name、props.size、props.transform 的地方出现此错误

【问题讨论】:

    标签: reactjs typescript styled-components react-props


    【解决方案1】:

    所以目前在 TypeScript 中没有办法将泛型类型传递给标记的模板文字

    有几个选项。首先是提供你第一​​次使用 props 的类型,然后 ts 会假设其余的都使用该类型

    背景:${(props: Props} => props.theme.brand}; 边框:${props => ...}

    或者使用这个 withProps 函数

    https://github.com/styled-components/styled-components/issues/630#issuecomment-317277803

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2019-08-18
      • 2019-09-13
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多