The concept of inheritance in CommonJS using Typescript

While experimenting with Ts, I encountered a roadblock

"use strict";

declare const require: any;

const EventEmitter : any = require('events').EventEmitter;

class Foo extends EventEmitter{ //*error* Type 'any' is not a constructor function type.

    constructor() {
        super();
    }
}

I even attempted to link EventEmitter to an interface type of mine, but faced the same issue.

Is there a way to expand a class using Typescript with commonjs modules?

Appreciate your help

Answer №1

experiment

"use strict";

import { EventEmitter } from 'events';

class Bar extends EventEmitter {
    constructor() {
        super();
    }
}

You might consider adding type definitions for the 'events' module:

npm install --save @types/node


Update

You can achieve the same using require:

"use strict";

import events = require('events');
const EventedEmitter = events.EventEmitter;

class Bar extends EventedEmitter {
    constructor() {
        super();
    }
}

Avoid relying on : any in general to benefit from IntelliSense and compile-time type checking

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

Transformation of classes in TypeScript to be more specific

Currently, I'm engaged in a project that requires the creation of multiple instances of a specific class with varying parameters. To simplify this task, I am developing a utility function called withAlteredConstructorArgs. However, I seem to be strugg ...

Tips for streamlining a conditional statement with three parameters

Looking to streamline this function with binary inputs: export const handleStepCompletion = (userSave: number, concur: number, signature: number) => { if (userSave === 0 && concur === 0 && signature === 0) { return {complet ...

Assign an appropriate label to this sonarqube input field

Sonarqube flagged an issue with the following line of code: <div class="dropdown-language"> <label>{{'GENERALE.LINGUA' | translate }}</label> <select #langSelect (change)="translate.use(langSe ...

Tips for accessing touch events within the parent component's area in React Native

I implemented the code below in my React Native app to disable touch functionality on a specific child component. However, I encountered an issue where the touch event was not being detected within the area of the child component. How can I fix this prob ...

Retrieve values of properties from an array

How can I retrieve property values from an array? const d = [{id: 'Cat'}, {id: 'Dog'}] type ids = ??? //place code here, type should be 'Cat' | 'Dog' (It would also be acceptable if it creates a const enum) ...

Converting an array of objects into a TypeScript dictionary with IDs as the key mapping

Is there a way to provide type hints for better autocompletion when accessing keys like dictionary.Germany from the following data and types? type Entry = { tld: string; name: string; population: number; }; const data: Entry[] = [ {tld: 'de&a ...

Can template literal types be utilized to verify if one numeric value is greater than another?

I am attempting to define the Record for migration functions, which use the direction of the migration as the key: v${number}-v${number}, Considering that these migrations are all UP, they need to be validated as v${first-number}-v${first-number + 1} and ...

Having trouble binding to the *ngFor directive?

I encountered an issue while attempting to utilize the *ngFor directive in my code. Upon running the application, I'm receiving an error message that is puzzling me. I have meticulously verified that all property names are correctly set and there are ...

Issue found in Next.js 14: Hydration Error | Caution: Client Components can only receive plain objects from Server Components

I'm currently in the process of creating an application using Next.js 14, TypeScript, Mongoose, and MongoDB. In this app, I retrieved user data from my database and displayed them on cards along with relevant information like tags represented by badg ...

What is the best way to fill the dropdown options in every row of a data table?

This HTML snippet displays a data table with test data and corresponding dropdown options. <tr> <th> Test Data </th> <th> Test Data ...

Show information in a table based on a unique identifier

I am working with some data that looks like this [ { date: '20 Apr', maths: [70, 80.5, 100], science: [25, 20.1, 30] }, { date: '21 Apr', maths: [64, 76, 80], science: [21, 25, 27] }, ]; My goal is to present ...

Can you please identify the TypeScript type associated with the return value of the match() method in String prototype?

I need help with creating a Regex in JavaScript const pattern = /S(\d+)E(\d+)/; // identifying characters between "S" and "D" const result = 'SE01E09'.match(pattern); How should I declare the result variable? I have attempted various ...

Setting up an OR guard in NestJS is a crucial step in managing

I need to secure a controller route using Guards, including IsAuthentifiedGuard, HasRoleGuard, and IsSafeGuard. I want the route to be accessible if at least one of these conditions is met: IsAuthentifiedGuard and HasRoleGuard pass IsSafeGuard passes For ...

Importing ES6 modules in TypeScript using absolute paths

I've been on a quest for hours trying to figure out how to import local ES6 modules using an absolute path. Every time I attempt to import something in a module like : import { Module } from "src/my-module"; I keep getting the error "Error: Cannot f ...

Add a new child component template with each click using the onclick event in Angular

Is there a way to dynamically add a child component with each button click event? Here is the HTML code for the button: <button type="button" class="btn btn-success btn-sm btn-add-phone" (click)="addfield()"><span class="fa fa-plus"></span ...

Limiting Users to Select Up to 5 Options in Angular Material Design's <mat-select multiple> Feature

Is it possible to limit the user to selecting only up to 5 options in a <mat-select multiple>? How can I achieve this? I tried using the selectionChange event, but I couldn't find a way to visually prevent the user from selecting more than 5 op ...

Received an unexpected argument count of 1 instead of the expected 0 while passing a function as a prop to a child component

I transferred the deleteImgfunc function from the insertFarmDiaryDetail component to the InsertFarmDiarySubPage component, which acts as a child component. DeleteImgfunc is a parameter-receiving function. Despite creating an interface and defining paramet ...

The Component TSX is reporting a Type error stating that the Property 'onChange' is not present in the type '{ key: string; value: Model; }' as required by Props

While running the following command: npm run build I encountered an error that needs resolution: /components/Lane.tsx:37:18 Type error: Property 'onChange' is missing in type '{ key: string; value: StepModel; }' but required in type &a ...

Adding FormControl dynamically to FormGroup can be achieved by simply using the appropriate method

Currently, I am working with a plunker where I am dynamically creating form components based on the model specified in app.ts. However, I am facing an issue where I cannot add formControlName = "name" to the component. In my control-factory.directive.ts ...

Incorporate Material Design Icons into your NPM Electron React application for sleek visuals

I am currently exploring how to incorporate Material Design Icons into an NPM Electron project with Webpack 4. The Google Github page suggests that the icons can be easily installed using npm install material-design-icons. However, based on this discussion ...