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);
}
}
This question is related to
angular
typescript
properties
Here is another simple approach, I used;
inputChange(event: KeyboardEvent) {
const target = event.target as HTMLTextAreaElement;
var activeInput = target.id;
}
Use currentValue
instead, as the type of currentValue is EventTarget & HTMLInputElement
.
Here is one more way to specify event.target
:
import { Component, EventEmitter, Output } from '@angular/core';_x000D_
_x000D_
@Component({_x000D_
selector: 'text-editor',_x000D_
template: `<textarea (keyup)="emitWordCount($event)"></textarea>`_x000D_
})_x000D_
export class TextEditorComponent {_x000D_
_x000D_
@Output() countUpdate = new EventEmitter<number>();_x000D_
_x000D_
emitWordCount({ target = {} as HTMLTextAreaElement }) { // <- right there_x000D_
_x000D_
this.countUpdate.emit(_x000D_
// using it directly without `event`_x000D_
(target.value.match(/\S+/g) || []).length);_x000D_
}_x000D_
}
_x000D_
fromEvent<KeyboardEvent>(document.querySelector('#searcha') as HTMLInputElement , 'keyup')
.pipe(
debounceTime(500),
distinctUntilChanged(),
map(e => {
return e.target['value']; // <-- target does not exist on {}
})
).subscribe(k => console.log(k));
Maybe something like the above could help. Change it based on the real code. The issue is ........ target['value']
Open tsconfig.json and disable strictDomEventTypes.
"angularCompilerOptions": {
....
........
"strictDomEventTypes": false
}
Here is the simple approach I used:
const element = event.currentTarget as HTMLInputElement
const value = element.value
The error shown by TypeScript compiler is gone and the code works.
consider $any()
<textarea (keyup)="emitWordCount($any($event))"></textarea>
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.
I believe it must work but any ways I'm not able to identify. Other approach can be,
<textarea (keyup)="emitWordCount(myModel)" [(ngModel)]="myModel"></textarea>
export class TextEditorComponent {
@Output() countUpdate = new EventEmitter<number>();
emitWordCount(model) {
this.countUpdate.emit(
(model.match(/\S+/g) || []).length);
}
}
Best way is to use templating => add id to your input and then use it value
<input type="text" #notaryLockup (keyup) = "searchNotary(notaryLockup.value)"placeholder="Entrez des information" >
searchNotary(value: string) {
// your logic
}
this way you will never have Typescript error when strict verification is activated => See angular Docs
In my case, I had:
const handleOnChange = (e: ChangeEvent) => {
doSomething(e.target.value);
}
And the issue was that I did not provide a type argument to ChangeEvent so that it knows e.target
was an HTMLInputElement
. Even if I manually told it that target
was an input element (e.g. const target: HTMLInputElement = e.target
), the ChangeEvent
still didn't know that made sense.
The solution was to do:
// add type argument
const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
doSomething(e.target.value);
}
Source: Stackoverflow.com