Expanding an abstract class with Typescript

In order to create a new instance of a base abstract class that implements an interface, I have the interface, the base class, and some properties in an Angular component. Surprisingly, no errors are thrown in the browser (perhaps due to JavaScript's forgiving nature), but TSLint raises the following error:

TS2511:Cannot create an instance of the abstract class KitCustomizationBase

If the abstract keyword is removed, the error disappears. According to the TypeScript documentation, abstract classes can contain implemented logic. Can someone clarify this for me?

Here is my interface:

export interface KitCustomizationInterface {
  items: number[];
  indexes: number[];
  counter: number;
}

This is my base class (the same error occurs even without the constructor):

import { KitCustomizationInterface } from '...';

export abstract class KitCustomizationBase implements KitCustomizationInterface {
  public items: number[];
  public indexes: number[];
  public counter: number;

  constructor(items: number[], indexes: number[], counter: number) {
    this.items = items;
    this.indexes = indexes;
    this.counter = counter;
  }
}

And here is the code snippet from my component:

...
let k: KitCustomizationBase = new KitCustomizationBase([], [], 0);
...

Update

Regarding the duplicate markers:

  1. Angular and TypeScript handle interfaces differently.
  2. This specific TSLint error doesn't yield relevant results in Google search.
  3. The provided link by the duplicate markers does not offer a precise answer, rather suggesting to refer to the documentation. But which documentation exactly?

Answer №1

Your actions are contradictory in nature. By declaring a class as abstract, you are clearly stating that it cannot be instantiated. Yet, you proceed to instantiate the class anyway. It's important to make a decision and stick to it.

  • If you wish to prevent instantiation of your class, then refrain from doing so. Instead, create a new class that extends your existing one and instantiate that instead. In TypeScript, you can also use structural typing to assign objects that are compatible with your variable without needing a constructor or even a class - an interface will suffice.
  • On the other hand, if you do want to instantiate your class, then it should not be marked as abstract. The purpose of an abstract class is to disallow instantiation altogether.

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

Data from graphql is not being received in Next.js

I decided to replicate reddit using Next.js and incorporating stepzen for graphql integration. I have successfully directed it to a specific page based on the slug, but unfortunately, I am facing an issue with retrieving the post information. import { use ...

Utilizing Mathematical Calculations Across Multiple JavaScript Functions

Just dipping my toes into the Javascript realm and attempting to crack this task my instructor assigned. Here's what he expects from us: Create a function to kickstart the program. Name it start() Inside the start() function, invoke a function n ...

Issue with ng-change function causing data not to load properly

I have added a data-ng-change='getSubjectsClasswise(classBean.class_id);' in the class <select> tag, but for some reason the subjects are not loading in the subject <select> tag. I have checked everything and it all seems fine, b ...

The design of RESTful web applications with a client-server architecture

I need clarification on how client-server architecture should function for a modern web application with a RESTful backend. For a web app, the client is typically the browser while the server is the web server. Programatically speaking, there are componen ...

Creating a data table with material design and integrated form features to store and save records - a step-by

Encountering numerous errors Unable to read property 'toUpperCase' of undefined (" ]*matRowDef="columns:displayedColumns" > For dynamically adding rows to the table <button (click)="addABunch(3)">Add 3</button ...

Creating a Fixed-length Array in TypeScript

Seeking assistance with TypeScript types, I have a particular question. When declaring a type for an array as follows... position: Array<number>; ...it allows for creating an array of any length. However, if you need an array containing numbers of ...

Create Office Script calculations with quotations included

I am currently attempting to create an Excel office script formula for a cell. Are there any tips on how to insert a formula with quotes into a cell? For example, the following works fine: wsWa.getCell(WaRangeRowCount, 9).setFormula("= 1 + 1"); ...

Vue + TypeScript prop type issue: "'Foo' is intended as a type, but is being treated as a value in this context."

As a newcomer to TypeScript and the Vue Composition API, I encountered an error that left me puzzled: I have a component that requires an api variable as a prop, which should be of type AxiosInstance: export default defineComponent({ props: { api: A ...

The Model Viewer module is unable to load the three-dimensional model

Attempting to incorporate a 3D model into my website using the modelviewer library, but encountering difficulties in loading the file as shown below: Just to note, I am utilizing github pages with a custom domain from godaddy which may be contributing to ...

The module script failed to load due to an unexpected response from the server, which was MIME type of text/jsx instead of a javascript module script

I have recently set up an express server and created an API, as well as installed React using Vite for my frontend. However, when I attempt to connect or load my main HTML file to the server, an error is displayed in the console. This is all new to me as I ...

Using Javascript, load a URL by making a JQuery ajax GET request and specifying custom headers

I currently have a small single-page application (SPA) using JQuery/Ajax for the frontend and Node/Express for the backend. The user authentication and authorization are handled with JSON-Webtoken, but I encountered an issue. If someone tries to access the ...

PHP-generated HTML onclick attribute malfunctioning

Here is the PHP code I am currently working with: $listing .= "<button onclick='updateValue(".$id.", ".$key.")'>change value</button>"; The variable $id is an integer and functions correctly. However, $key is a reference from a for ...

UCS-2 in Node.JS: Understanding Big-Endian Byte Order

Currently, I am utilizing Node.JS. In my project, I require support for big-endian UCS-2 buffers, which is not natively offered by Node's buffers that only support little-endian format. How can I achieve this specific requirement? ...

Tips for removing < and > symbols from the XML response on the client side

I have received a response from the server in XML format, containing partial information as shown below: <list> <Response> <cfgId>903</cfgId> <recommendations> &lt;Rule&gt; ...

Preserve the location of a moveable div using jQuery

I have implemented draggable divs in my JSP page, allowing users to move them around freely. After dragging the divs, I would like to save their positions either in a cookie or database for future reference. Could you please advise on the best way to ach ...

Next.js: Uh-oh! Looks like we've hit an obstacle with Error 413 Request Entity

I've encountered an issue while attempting to upload large files using Next.js. I've set up an onChange function for my file input, and here's the code snippet: const handleFileUpload = () => new Promise(async (resolve) => { if(ref ...

There seems to be an issue populating the array

My code includes an ajax call that looks like this: $.ajax({ type: 'POST', url: '../ws_BQS.asmx/ResultadosDimensionalByDate', data: '{"fecha":"' + fecha + '"}', dataType: 'json', conte ...

Leveraging classes in Selenium for eliminating repetitive code

My task involves opening two websites that are essentially the same, but with different URLs, usernames, and passwords for logging in. Currently, I have two functions with duplicate code manually sending two different sets of user credentials. I would lik ...

Modify the text color of the fixed navigation when hovering over specific divs

Sorry if this question has already been asked. I've searched online but haven't come across a satisfactory solution. On my website, I have different background colors - blue and white. The text color in my navigation is mostly white, but I want i ...

Organize information before making a choice in the dropdown menu

Currently, I have a dropdown menu in HTML that allows you to order the data by different options: <select ng-model="sortOrder" ng-options="option.value as option.name for item in options"></select> In addition, I have implemented code using n ...