Property 'value' does not exist on type 'EventTarget'

前端 未结 7 2083
深忆病人
深忆病人 2020-11-30 00:40

I am using TypeScript Version 2 for an Angular 2 component code.

I am getting error "Property \'value\' does not exist on type \'EventTarget\'" for below co

7条回答
  •  失恋的感觉
    2020-11-30 01:36

    Since I reached two questions searching for my problem in a slightly different way, I am replicating my answer in case you end up here.

    In the called function, you can define your type with:

    emitWordCount(event: { target: HTMLInputElement }) {
      this.countUpdate.emit(event.target.value);
    }
    

    This assumes you are only interested in the target property, which is the most common case. If you need to access the other properties of event, a more comprehensive solution involves using the & type intersection operator:

    event: Event & { target: HTMLInputElement }
    

    You can also go more specific and instead of using HTMLInputElement you can use e.g. HTMLTextAreaElement for textareas.

提交回复
热议问题