最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property 'value' does not exist on type EventTarget (ts2339) - Stack Overflow

programmeradmin3浏览0评论

I am getting squigglies:

You can see I'm typing it right (I think).

  const hClick = (e:React.MouseEvent<HTMLButtonElement>)=>{
    console.log(e.target.value)
  }

Here's the element in question.

      {props.question.responses.map((r: string, i: number) => {
        return <button key={i} value={r} className={`${CSS.columnBtn} ${CSS.gray}`} onClick={(e) => hClick(e)}>{r}</button>;
      })}

You can see it clearly has a value. I also did my research. Not only do buttons have values natively but MS said in their docs they have a value prop as does the general button element per Mozilla and when I ctrl + clicked into the d.ts file for HTMLButtonElement, it shows a value prop that's a string:

    /**
     * Sets or retrieves the default or selected value of the control.
     */
    value: string;

Morever the code works and it logs the value. I'm pretty new to TypeScript so there's probably something I don't know, and that's what I want to learn.

BTW if a property doesn't exist in a given element but you give it a custom one, it obviously won't be in the d.ts so how would you tell the compiler that your html attribute foo[="bar"] exists on the event?

I am getting squigglies:

You can see I'm typing it right (I think).

  const hClick = (e:React.MouseEvent<HTMLButtonElement>)=>{
    console.log(e.target.value)
  }

Here's the element in question.

      {props.question.responses.map((r: string, i: number) => {
        return <button key={i} value={r} className={`${CSS.columnBtn} ${CSS.gray}`} onClick={(e) => hClick(e)}>{r}</button>;
      })}

You can see it clearly has a value. I also did my research. Not only do buttons have values natively but MS said in their docs they have a value prop as does the general button element per Mozilla and when I ctrl + clicked into the d.ts file for HTMLButtonElement, it shows a value prop that's a string:

    /**
     * Sets or retrieves the default or selected value of the control.
     */
    value: string;

Morever the code works and it logs the value. I'm pretty new to TypeScript so there's probably something I don't know, and that's what I want to learn.

BTW if a property doesn't exist in a given element but you give it a custom one, it obviously won't be in the d.ts so how would you tell the compiler that your html attribute foo[="bar"] exists on the event?

Share Improve this question asked Mar 5, 2021 at 1:46 gcrgcr 4632 gold badges7 silver badges18 bronze badges 1
  • 1 What if you use currentTarget instead? – MinusFour Commented Mar 5, 2021 at 1:52
Add a comment  | 

2 Answers 2

Reset to default 15

Since target could be any other potential element (or even a non-element) you can use a Type Assertion to make it into HTMLButtonElement.

const hClick = (e:React.MouseEvent<HTMLButtonElement>)=>{
    let button = e.target as HTMLButtonElement;
    console.log(button.value)
}

I look on the React Typings and the HTMLButtonElement will end up in currentTarget (which makes sense because that's the element the listener is attached to which is a button).

So you can just use:

const hClick = (e:React.MouseEvent<HTMLButtonElement>)=>{
    console.log(e.currentTarget.value)
}

<input (keyup)="myeventreal($any($event).target.value)" />

use this in angluar 13 version

发布评论

评论列表(0)

  1. 暂无评论