【问题标题】:Typescript: Type 'string' is not assignable to custom type打字稿:类型“字符串”不可分配给自定义类型
【发布时间】:2019-07-12 05:03:18
【问题描述】:

我有这样的类型,

interface StyleProps {
  display?: 'hide' | 'active'
}

并且该类型正在被下面的组件使用

<Section display={`${this.state.section !== 'chatbot' ? 'hide' : 'active'}`}>

display 只能是 hideactive 但我仍然收到此错误

 TS2322: Type 'string' is not assignable to type '"hide" | "active" | undefined'.

有什么方法可以查看display 中的内容或我做错了什么?

【问题讨论】:

    标签: reactjs typescript typescript-typings typescript2.0 styled-components


    【解决方案1】:

    如果您删除模板文字并直接传入'hide''active',则可以消除此错误。

    <Section display={this.state.section !== 'chatbot' ? 'hide' : 'active'}>
    

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 2022-01-17
      • 2020-02-07
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2019-04-10
      • 2021-12-29
      相关资源
      最近更新 更多