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 code, what could be the solution. Thanks!
e.target.value.match(/\S+/g) || []).length
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'text-editor',
template: `
<textarea (keyup)="emitWordCount($event)"></textarea>
`
})
export class TextEditorComponent {
@Output() countUpdate = new EventEmitter<number>();
emitWordCount(e: Event) {
this.countUpdate.emit(
(e.target.value.match(/\S+/g) || []).length);
}
}
Best Answer
You need to explicitly tell TypeScript the type of the HTMLElement which is your target.
The way to do it is using a generic type to cast it to a proper type:
or (as you like)
or (again, matter of preference)
This will let TypeScript know that the element is a
textarea
and it will know of the value property.The same could be done with any kind of HTML element, whenever you give TypeScript a bit more information about their types it pays you back with proper hints and of course less errors.
To make it easier for the future you might want to directly define an event with the type of its target: