react+ts 传参类型问题

子组件

props:{
   type:'count' | 'time'

}

调用

<component type='count'/> //正确

const a='count'
<component type={a}/>// 错误 无法将string类型赋予 count | time 

这个问题该如何解决 type 是动态的

阅读 3k
1 个回答

指定类型:

const a: 'count' | 'time' = 'count';

嫌啰嗦可以写个 type

type ComponentType = 'count' | 'time';

// 子组件
props: {
   type: ComponentType 
}

// 父组件
const a: ComponentType = 'count';
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题