Lev
Lev

Reputation: 15744

add a property ton an existing class

I have a function that takes as argument

event: MouseEvent

than in my code if I do

event.srcElement.innerText;

intelisense tells me

[ts] Property 'innerText' does not exist on type 'Element'.

although the property does exist.

How can I add property innerText to MouseEvent withthout creating a new class that extends MouseEvent ?

Upvotes: 0

Views: 159

Answers (1)

Saravana
Saravana

Reputation: 40712

Cast it to HTMLElement:

(event.srcElement as HTMLElement).innerText;

Or modify your function declaration to specify that srcElement is HTMLElement:

function test(event: MouseEvent & {
    srcElement: HTMLElement
}) {
    event.srcElement.innerText;
}

The parameter event is an intersection type now.

Upvotes: 3

Related Questions