属性“名称”在类型“ EventTarget”上不存在-React + TypeScript

gpbaculio

这是我的代码:

<Button
  disabled={filter === 'Active'}
  size='md'
  color='primary'
  name='Active' // complete = false
  onClick={this.handleFilterClick}
>
  Active
</Button>

在我的函数处理程序上,我在事件上出错:

handleFilterClick = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
    // continue here
     const { name } = e.target;

它说:

Property 'name' does not exist on type 'EventTarget'.

我也尝试过:

(e:  React.MouseEvent<HTMLInputElement, MouseEvent> &  React.MouseEvent<HTMLButtonElement, MouseEvent>) 

按钮的事件类型是什么?在JavaScript中,这种方法可以接受名称,但是我不知道为什么不输入打字稿?

阿格尼

event.target是从中调度元素的元素,不一定必须HTMLButtonElement在事件中进行定义。

但是,如果使用event.currentTarget,您将看到此错误消失了:

const { name } = event.currentTarget;

如果必须使用event.target自身,则必须强制转换对象:

const { name } = event.target as HTMLButtonElement;

从打字:

/**
  * currentTarget - a reference to the element on which the event listener is registered.
  *
  * target - a reference to the element from which the event was originally dispatched.
  * This might be a child element to the element on which the event listener is registered.
  * If you thought this should be `EventTarget & T`, see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12239
*/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

与 TypeScript 反应 - 类型“EventTarget”上不存在属性“value”

类型'EventTarget'上不存在属性'value'。React.SyntheticEvent <HTMLElement>中不存在“值”

TypeScript错误TS2339:类型“ EventTarget”上不存在属性“ matches”

Typescript - Solid.js <select> onchange 事件:“EventTarget”类型上不存在属性“值”

TypeScript中的EventTarget类型不存在属性“值”

TypeScript中的“ EventTarget”类型错误不存在属性“文件”

Typescript抱怨PostResponse []类型上不存在属性名称

React组件中的TypeScript TS2339错误:类型“ IntrinsicAttributes ...”上不存在属性“ xyz”

属性'ref'在类型'A'上不存在。ts(2339)-React,TypeScript

React TypeScript & ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref”

TypeScript属性“ value”在类型“ HTMLElement”上不存在。React Jest测试

如何删除 React Typescript 中类型“{}”上不存在的属性“长度”

属性“位置”在类型“只读<{}>”上不存在-React Router和Typescript

TS2339:移植到Typescript时,类型'typeof React'上不存在属性'PropTypes'

类型/ IntrinsicAttributes和IntrinsicClassAttributes上不存在React Typescript属性

React Typescript自定义挂钩:类型“ {}”上不存在属性“ prop_name”

React-TypeScript:类型“IntrinsicAttributes 和 IntrinsicClassAttributes<GoogleMapReact> 上不存在属性“位置”

数组映射期间类型上不存在 React Typescript 属性

React&Typescript:类型“ number”上不存在属性“ id”

类型“{}”上不存在属性“道具”(试图访问子项的道具)-React Typescript

在 React / Typescript 中上传文件->“从不”类型上不存在属性“点击”

TypeScript - 类型上不存在属性(但它存在)?

新的Typescript 1.8.4构建错误:“构建:类型'EventTarget'不存在属性'result'。”

TypeScript:禁止联合类型的“类型上不存在属性”

TypeScript React中的可重用布局组件:类型“ IntrinsicAttributes&IProps”上不存在属性“路径”

在 Typescript 中重写为 React 功能组件时出错:类型 'MutableRefObject<Spinner | 上不存在属性 'forceUpdateHandler' 空>'

与 Typescript 反应:“DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>”类型上不存在属性“名称”

KeyboardEvent:类型“ EventTarget”上不存在属性“值”

类型“ EventTarget”上不存在属性“数据集”