打字稿:类型“字符串”不可分配给自定义类型

时间:2019-02-18 18:48:31

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

我有这样的类型

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中的内容吗?

1 个答案:

答案 0 :(得分:2)

如果删除模板文字并直接传递'hide''active',则可以避免此错误。

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