I have a function that takes an argument with two types.
Refer to the typsescript docs, this SO question, and this SO question related to TS functions and types.
Function
public onKeydown(ev: KeyboardEvent | EventTarget): void {
if (ev.keyCode === 13) {
this.sendMessage();
ev.target.blur();
} else {
this.chatService.emitTyping();
}
return;
}
HTML for the page
<textarea
autosize
[minRows]="1"
[maxRows]="4"
[(ngModel)]="message"
placeholder="escribe aqui"
class="msg-input"
(keydown)="onKeydown($event)"
(click)="scrollToBottom()"
></textarea>
I cannot see that the function onKeyDown
has two different types. Because the or
doesn't seem to have the same effect in Angular as the docs of Typescript say. For example, if the 'Enter' key is pressed then blur()
the keyboard and send the message. Am I going to have to make a separate function just to blur the keyboard?
This is the error I am getting:
ERROR in src/Components/chat/chat.component.ts(72,12): error TS2339: Property 'keyCode' does not exist on type 'EventTarget | KeyboardEvent'.
[ng] Property 'keyCode' does not exist on type 'EventTarget'.
[ng] src/Components/chat/chat.component.ts(74,10): error TS2339: Property 'target' does not exist on type 'EventTarget | KeyboardEvent'.
[ng] Property 'target' does not exist on type 'EventTarget'.
as
later? basarat.gitbooks.io/typescript/docs/types/type-assertion.html – JGFMK