Include a new attribute to an already defined class

I'm working with a function that requires the argument

event: MouseEvent

If I try to access

event.srcElement.innerText;

the error message pops up saying

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

even though the property exists.

Is there a way to add the innerText property to MouseEvent without having to create a new class that extends MouseEvent?

Answer №1

Convert it to an HTMLElement:

(event.srcElement as HTMLElement).innerText;

Alternatively, update your function declaration to explicitly state that srcElement is of type HTMLElement:

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

The parameter event has now become an intersection type.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Angular and Typescript to Submit a Post Request

I am working on a basic Angular and Typescript page that consists of just one button and one text field. My goal is to send a POST request to a specific link containing the input from the text field. Here is my button HTML: <a class="button-size"> ...

Error: The function createImageUrlBuilder from next_sanity__WEBPACK_IMPORTED_MODULE_0__ is not defined as a valid function

Having some trouble with my Next.js TypeScript project when integrating it with sanity.io. I keep getting an error saying that createImageUrlBuilder is not a function. See screenshot here Here is the code from my sanity module ...

The Angular subscription gets triggered multiple times

I am currently subscribed to this service: login(): void { if (!this.loginForm.valid) { return; } const { email, password } = this.loginForm.value; this.authService.login(email, password).subscribe((res) => { if (res.ok) ...

Having trouble with an unexpected value in your Angular2 Service? Don't forget to add

I encountered an error in my Angular2 app: Error: (SystemJS) Unexpected value 'ReleasesService' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation. Here is my AppModule code: import { NgModule } fr ...

How can we retrieve and display the value of a property within an object nested in a FormControl within a FormGroup?

Encountering an issue with form control. I have developed a reactive form consisting of 2 controls - an object and a formArray. However, when I bind the "start" control to the HTML, it displays as [Object Object] instead of binding to the address property ...

Error message arising from a change in expression after it has been verified, specifically when using the ngClass directive

I've been attempting to add an active class to my route using a custom logic, but it seems that routerLinkActive is not working due to the dynamic nature of my parent and child routes. Here's the code snippet in question: <ul class="navbar-na ...

The functionality of absolute import seems to be malfunctioning in React Vite js

Issue with vite config not importing from src folder error message: Failed to resolve import "redux/1space/letter/inboxSlice" from "src/pages/letter/index.jsx". Is the file present? / import { loadInboxs } from "redux/1space/lette ...

What is the best way to properly mock certain methods within a Jest class?

Imagine having a class structure like this: located at ./src/myClass.ts class myClass{ methodA(){ ... } methodB(){ ... } } Now, let's say I need to mock method A. To do this, I created a file ./src/mocks/myClass.ts class ...

Customize the color of a specific day in the MUI DatePicker

Has anyone successfully added events to the MUI DatePicker? Or does anyone know how to change the background color of a selected day, maybe even add a birthday event to the selected day? https://i.stack.imgur.com/or5mhm.png https://i.stack.imgur.com/so6Bu ...

Turn off the feature that prohibits the use of variables before they are

I'm struggling to disable this setting in my tsconfig file. The TS documentation doesn't seem to have any information about how to do it. no-use-before-declare I'm facing an issue where my test stub data objects are interfering with each ot ...

Limit the type to be used for a particular object key in TypeScript

My pet categories consist of 'dog' and 'cat' as defined in the Pet type: type Pet = 'dog' | 'cat' I also have distinct types for allowed names for dogs and cats: type DogName = 'Jack' | 'Fenton&apos ...

Property missing in Typescript type definition

In my Typescript Next project, I am using this component: import PageTitle from './pagetitle' import style from './contact.styl' export default function Contact() { return ( <section> <a name="contact"> ...

What is the process for triggering property decorators during runtime?

Wondering about dynamically invoking a property decorator at runtime. If we take a look at the code snippet below: function PropertyDecorator( target: Object, // The prototype of the class propertyKey: string | symbol // The name of th ...

Utilize localStorage.getItem() in conjunction with TypeScript to retrieve stored data

Within my codebase, I have the following line: const allGarments = teeMeasuresAverages || JSON.parse(localStorage.getItem("teeMeasuresAverages")) || teeMeasuresAveragesLocal; Unexpectedly, Typescript triggers an alert with this message: Argument ...

Challenge with React CloneElement regarding typing

Utilizing React Children and React Clone element, I aim to trigger methods in both the wrapper and Select components upon onClick event in the Option component. Although everything is functioning correctly, I am encountering a type error when calling the O ...

Verify if the Contact Number and Email provided are legitimate

Is it possible to determine if the phone number or email entered by a user is valid or not? Using Angular 7 and Firebase? ...

Transferring data from index.ts using export and import operations

While working on my angular project, I encountered a rather peculiar issue. I have organized my project into separate modules for layouts, login, and dashboard. Specifically for the login page, I wanted to implement a unique layout. Here's how I tried ...

Which flow is best for single page applications: Implicit or Authorization Code in OpenID Connect?

OIDC offers multiple authentication flows, with Implicit and Auth Code flow being the two primary options available for SPAs. Recent discussions in the ietf mailing list suggest that Auth code flow is preferable to implicit flow due to security concerns su ...

Encountering a problem with Typescript and eslint while utilizing styled-components and Material UI: "Warning: React does not identify the `showText` prop on a DOM element."

While using a styled component to return a material ui Fab component, an error appears in the console: React does not recognize the `showText` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as low ...

Application Initialization Error: appInits is not a valid function

When my Angular v17 application starts, I need to set some important values right away. This is how it's done in app.config.ts: export const appConfig: ApplicationConfig = { providers: [ ConfigService, ... { pr ...