简体   繁体   English

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

[英]Typescript: Type 'string' is not assignable to custom type

I have a type like this, 我有这样的类型

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

and the type is being used by component below 并且该类型正在由下面的组件使用

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

display can only be hide or active but I'm still getting this error display只能是hideactive但我仍然收到此错误

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

Is there any way to see what's in display or anything im doing it wrong? 有什么办法可以查看display或我做错了什么吗?

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

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

暂无
暂无

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

相关问题 打字稿:类型“字符串”不可分配给类型 - Typescript: Type 'string' is not assignable to type TypeScript 错误:类型“字符串”不可分配给排版类型 - TypeScript error: Type 'string' is not assignable to type for Typography React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]' Typescript React - 类型“字符串”不可分配给类型“外观” - Typescript React - Type 'string' is not assignable to type 'Appearance' TypeScript:“字符串| 未定义”不能分配给“字符串”类型 - TypeScript: 'string | undefined' is not assignable to type 'string' 字符串类型别名不可分配给带有 typescript 的 Recoil 中的字符串 - string type alias is not assignable to string in Recoil with typescript 节点打字稿:键入 &#39;string | string[]&#39; 不可分配给类型 &#39;string&#39; - Node typescript: type 'string | string[]' is not assignable to type 'string' Typescript 和上下文,类型不可分配 - Typescript and context, Type is not assignable React,typescript - 'string | 类型的参数 null' 不能分配给“字符串”类型的参数 - React, typescript - Argument of type 'string | null' is not assignable to parameter of type 'string' TypeScript - “字符串”类型的参数不可分配给“字符串”类型的参数 - TypeScript - Argument of type 'String' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM