Is there a way to avoid using the super() constructor unnecessarily in Eslint?

code:

  constructor(cdr: ChangeDetectorRef, inj: Injector) {
    super(cdr, inj);
  }

eslint:

 {
   ...
   overrides: {
      ...
      rules: {
        ...
        "@typescript-eslint/no-useless-constructor": "error",
        ...
      }
   }
}

The guideline instructs me to eliminate unnecessary constructor, yet I need to ensure super() is called within it.

Is there a way for me to adhere to this guideline without directly disabling or deleting it?

Answer №1

Simply omit the constructor if you have no need for it. In essence, it is redundant as JavaScript/TypeScript automatically handles it. For an extended class, not having a constructor is equivalent to having an empty one.

In cases where you do not define your own constructor, a default one will be provided for you. If your class is a subclass, the default constructor will invoke the parent constructor and pass any arguments along.

Therefore, constructors in derived classes should include a super call. However, it is not mandatory to create an empty constructor or simply use super inside (for derived classes), unless you intend to alter its functionality or signature.

If you wish to retain the constructor despite it being considered "useless," you can disable the linter rule that flags it.

It's important to note:

A lint rule may highlight constructors that only modify visibility of a parent constructor. -- Refer to the TS Docs and ES Docs for more information on this.

In Angular, having a seemingly trivial constructor with a super call is essential for certain scenarios like Injectable and Constructor (derived classes). Failure to include this could result in the error message:

This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
Hence, in such cases, the eslint rule must be turned off.

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

How do I search for and display a collection associated with another in Mongoose?

I am currently working on a task to locate and display all questions related to the "Question Collection" that are associated with all the surveys created by a specific user in the "Surveys Collection". Although I have successfully found questions for a pa ...

Issue with dropdown validation (button remains disabled)

I am working on creating a simple input form with validation requirements. Specifically, I want the button to be enabled or disabled based on the selection made in a dropdown menu. constructor(public fb: FormBuilder) { } /** * Form */ ...

Attitude: Defiant and Ignoring Authority

So far, no suggestions have been made, indicating that maybe I haven't properly summarized the issue; The problem arises when I absolutely position the section with the class="container" using an additional class or id specific to that <div>. I ...

React, incorporating emojis within a dropdown menu

Recently, I've been developing a small game using React where players can customize settings before beginning. The game involves four players chasing different tokens on the map while avoiding the player designated as "it". Within my code, I have a r ...

Issue with binding background images to DIV elements in Angular 4 CSS

Here is a template example: <div [style.background-image]="profileImage" ></div> In the TypeScript file: We declare private profileImage: any; and use DomSanitizer for security. Fetching photo from service: We set this.profileImage using b ...

Inhibit the ASP:dropdownlist when the value of another ASP:Dropdownlist is selected

Trying to modify two asp.net dropdownlists using client-side JavaScript is my current challenge. Specifically, these dropdowns are located within a modal that opens with a function. The objective is to disable the second dropdown (dropdown2) whenever the ...

What is the best way to incorporate a swf file into a website with full width and height using swfObject and spark?

When using swfObject to embed an object and setting the width and height values to 100%, I encountered an issue. Despite setting the outer div width and height to 500, the swf only expands to 100% of the width but not the height. html/javascript: ...

Deselect the checkboxes within an array

I've been grappling with a script to reset all checkboxes in a Google Sheet to unchecked as part of my daily cleanup routine. I've managed to identify and uncheck checkboxes in one sheet but am struggling to efficiently extend this to all sheets. ...

Steps to dynamically include a marker on a Google Maps component using HTTPGET in Angular 6

I am currently working on an Angular 6 application that involves integrating the Google Javascript API with AGM. So far, the map functions well except for dynamically adding markers using an http get request. Here is a snippet of the component.html: < ...

Setting up a project in TypeScript with Angular 2(+) and a Node/Express server is essential for successful

Searching for the optimal approach for a project similar to this: An Angular 2 (4) client coded in TypeScript A Node/Express backend also written in TypeScript Utilizing some shared (TypeScript) models accessible to both client and server code. Is it pr ...

Tips for obtaining the unique array element lengths in a JSON array

How can I determine the number of active users in a JSON array that includes both active and inactive user statuses? { "body": { "existing_users": [ { "product": "Pack-2", "user_statu ...

Implementing TypeScript in an Asp.net Core ReactJS application`s configuration

After using Visual Studio 2022 to create an asp.net core Reactjs project, I discovered that everything was written in javascript instead of typescript. Is there a way to switch this project over to typescript? ...

Setting up Storybook with Tailwindcss, ReactJS and Typescript: A comprehensive guide

What is the best way to configure Storybook to handle Tailwindcss styles and absolute paths? Just a heads up, this question and answer are self-documenting in line with this. It was quite the process to figure out, but I'm certain it will help others ...

Can dependencies be installed from multiple package.json files for monorepo projects using npm?

In my distribution folder, there is a main package.json file along with several sub-projects like project1, project2, etc. I need to create a tarball tgz file containing all of them. When someone installs the package from the tgz file, it should install ...

Implementing JavaScript functionality to read and interact with a Boolean field in a

Within my models.py file, I've defined the following: urgency = models.BooleanField(blank=True, default=False, verbose_name="Hitno otklanjanje") I am attempting to execute a javascript function that will modify a text field based on whether ...

Encountered an invalid prop type error while employing CSSTransition

Encountering an issue with the implementation of CSSTranstion for React. The purpose is to animate the mobile menu in a small application. Everything was functioning properly until the inclusion of the react-transition-group package, specifically using < ...

Error discovered in Webpack syntax

I'm a newcomer to webpack and feeling lost on how to properly configure the settings. Here is my project structure: / -- /public -- /js -- app.js -- /css -- app.scss -- /node_modules -- /autoprefixer -- /babel-loader ...

a guide to transforming data into a string with json using angular

I am struggling to figure out how to bind my form data into a JSON string. My situation involves using a string field in the database and saving checkbox values in a single database column using JSON. I have created an HTML form, but I am unsure of how to ...

options for adaptive web layout

My goal is to ensure my website is responsive by utilizing HTML5, CSS3, JavaScript, and jQuery. I am facing an issue with the responsiveness of my menu in the CSS code. PRÉSENTATION OFFRES PRODUITS RÉFÉRENCE CONTACT Menu nav { ...

Retrieve stock data using YQL and JQuery within an ASP.NET application

My ASP.NET project involves retrieving stock information using a YQL query. I am utilizing jQuery to make a JSON call in order to fetch the data. The problem arises when clicking the 'GetData' button on the page, as it simply refreshes the page w ...