我无法弄清楚如何将Focus事件与React和Typescript一起使用:
private onFocus(event: FocusEvent) {
}
...
<div
tabIndex={0}
onFocus={this.onFocus}
>
Element
</div>
使用上面的代码我得到以下错误
属性'onFocus'的类型不兼容 . 类型'(event:FocusEvent)=> void'不能分配给'((event:FocusEvent)=> void)|未定义” . 类型'(event:FocusEvent)=> void'不能赋值为'(event:FocusEvent)=> void' . 参数'event'和'event'的类型不兼容 . 类型'FocusEvent'不能分配给'FocusEvent'类型 . 'FocusEvent'类型中缺少属性'initFocusEvent' .
我尝试了一些方法来尝试使Typescript编译器开心,但似乎没有任何工作 .
1 回答
基于错误,您似乎使用了错误的FocusEvent类型 - 您正在处理React合成事件,因此您需要使用React类型 .
我对以下处理程序没有错误(您的结果可能因您的react.d.ts版本而异):