【发布时间】:2019-05-08 06:24:25
【问题描述】:
如何将自定义枚举类型注入 HTMLInputElement 的值?
我搜索了打字稿文档,但找不到这样做。
enum ValidColor {
'red',
'blue',
}
class paintStore {
wallColor: ValidColor = 'red';
onPaintClick = (e: React.ChangeEvent<HTMLInputElement>) => {
this.wallColor = e.target.value // Type 'string' is not assignable to type 'ValidColor'.ts(2322)
}
}
我尝试创建自定义类型但失败了。
interface ColorTarget {
value: ValidColor;
}
interface MyColor extends HTMLInputElement {
target: ColorTarget;
}
onPaintClick = (e: React.ChangeEvent<MyColor>) => {
this.wallColor = e.target.value // it is not working...
}
我该怎么做?
【问题讨论】:
标签: html reactjs typescript events enums