One of my functions requires an argument that can have one of two different types.
If you're interested in TypeScript functions and types, take a look at the official documentation, as well as these Stack Overflow questions: Question 1 and Question 2.
This is the function:
public onKeydown(ev: KeyboardEvent | EventTarget): void {
if (ev.keyCode === 13) {
this.sendMessage();
ev.target.blur();
} else {
this.chatService.emitTyping();
}
return;
}
This is the HTML for the page:
<textarea
autosize
[minRows]="1"
[maxRows]="4"
[(ngModel)]="message"
placeholder="Type your message here"
class="msg-input"
(keydown)="onKeydown($event)"
(click)="scrollToBottom()"
></textarea>
I'm struggling to incorporate the two different types within the onKeyDown
function properly. It seems that the use of '|
' does not behave in Angular as it should according to the TypeScript documentation. For instance, when the 'Enter' key is pressed, I want to blur the keyboard and send the message. Do I need to create a separate function just to blur the keyboard?
The error message I'm encountering reads:
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'.