Tips for adding comments to a class that extends in Angular (Inheritance)

With a plethora of child components such as Child1Component, how can the annotations be properly written for the extended parent class ParentComponent?

/** Parent Component */
/** <----- How to annotate here? */
export class ParentComponent implements OnInit {
  constructor(i: INJECTED1, otherParam: string) {
    ...
  }
  ...
}

/** Child Component */
@Component({selector: ..., template: ..., styleUrls: ...})
export class Child1Component extends ParentComponent {
  constructor(i: INJECTED1, j: INJECTED2) {
    super(i, 'otherParam');
  }
  ...
}

Previous attempts:

  • Trying to annotate ParentComponent with @Directive() seemed like a close match, but it felt incorrect as it's not truly an angular directive. Additionally, the linter did not approve without further file naming modifications.
  • Leaving ParentComponent unannotated resulted in the Angular Compiler throwing an error
    NG2007: Class is using Angular features but is not decorated
    .
  • Adding the annotation @Component({template: ''}) to ParentComponent seemed like a better solution and pleased the linter with the file name. However, it caused issues with the angular compiler expecting all parameters of ParentComponent to be injectables, which they're not since they are called by their superclass.

Answer №1

Indeed, utilizing @Directive for annotation is the recommended approach (particularly since Angular 9, if memory serves me right; earlier iterations of Angular didn't require it).

You have the option to change the ParentComponent name to ParentDirective and adjust the corresponding file name to satisfy the linter's preferences, or you could opt to configure the linter to overlook this particular scenario.

Edit: Personally, I lean towards renaming ParentComponent as ParentDirective, as upon reflection, a component essentially functions as a directive (i.e. a selector that prompts Angular to handle specific HTML tags or directives), but components come with templates and styles whereas directives do not. Considering your parent class lacks a template, it seems fitting to label it as a directive.

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

Utilizing SCSS variables

Currently, I am in the process of developing an Angular 4 application using angular-cli and have encountered a minor issue. I am attempting to create a component that has the ability to dynamically load styling. The ComponentX component needs to utilize a ...

Implementing inheritance: The step-by-step process of creating object variables

I want to define a variable called self._query within the class Hello, with certain conditions: The variable should be created when another class inherits from the class Hello. The variable declaration should happen inside the Hello class and not from ou ...

What is the best way to access the wrapped input data from an ion-input component in Ionic2?

Is there a way to connect an google.maps.places.Autocomplete to an ion-input in Ionic2? The issue is that in Ionic2, the ion-input wraps the <input> element, making it difficult to access. I attempted to solve this by creating a directive and utiliz ...

Issue with PrimeNG p-editor Appearance

My text editor is not displaying correctly on my website. Please refer to the following images for reference: Top of the page Bottom of the page Currently, it only shows a large SVG and a few input fields at the bottom. The technologies I am using includ ...

Vue 3 - Compelled to utilize any data type with computedRef

Recently, I've been diving into Vue/Typescript and encountered a puzzling error. The issue revolves around a class named UploadableFile: export class UploadableFile { file: File; dimensions: Ref; price: ComputedRef<number>; ... constr ...

Postgres Array intersection: finding elements common to two arrays

I'm currently developing a search function based on tags, within a table structure like this CREATE TABLE permission ( id serial primary key, tags varchar(255)[], ); After adding a row with the tags "artist" and "default," I aim ...

Mocking Dependencies in TypeScript with Jest

Here is the content of my index.ts file: import {IS3Client, S3Client} from './client/S3Client'; const s3: IS3Client = new S3Client(); export async function someFunc(event: any, context: any, callback: any) { const x: string = await s3.getFil ...

Is Angular 10 disregarding set-cookie response headers?

After logging in, I attempted to utilize a session auth cookie without success. To troubleshoot, I implemented a basic "CookieTest" method on my Dotnet Core server: [Route("CookieTest")] public ActionResult CookieTest() { var options = new CookieOptions ...

Filling a data entry with simultaneous commitments

Sample code: type Alphabet = 'a' | 'b' | 'c'; const alphabetMap: Record<Alphabet, null> = { 'a': null, 'b': null, 'c': null} // Select any asynchronous processing function you prefer funct ...

What causes the map function to behave differently when I use an array as the return value rather than a primitive in the callback function?

Program var corporations=[ {name:'Vicky',category:'Devdas',start:1993,end:2090}, {name:'Vikrant',category:'Devdas',start:1994,end:2019}, {name:'Akriti',category:'mental',start:1991,end:2021}, { ...

What could be causing the parsing errors in my React-Intl version 3.2.2?

After updating my app from reactinlt 2.x to version 3.2.2, I encountered a series of errors related to formatted messages: index.js:1437 [React Intl] Error occurred while formatting message: "titleBar.login" for locale: "de", default message used as fallb ...

The TypeScript @types version's compatibility with the Library version is interconnected

Seeking clarity on the versioning system used for TypeScript @types. Visit https://github.com/DefinitelyTyped/DefinitelyTyped For instance: I presumed that if I was utilizing [email protected], then I would need to install @types/[email protecte ...

The API requests for POST and DELETE are functional in POSTMAN, however they are not functioning properly when accessed through the browser

So in my Angular application, I'm working on a feature where users can add their favorite movies by fetching data from an API. To achieve this, I have set up a POST method in the API and integrated it into the front-end Angular app. Let's take a ...

Utilizing TypeScript's Exclude feature with a generic type

An update to dependencies in my TypeScript-based Nodejs project has led to compilation errors when working with a generic class that manages mongoose models. The root cause appears to be related to how TypeScript handles generic types. To illustrate the i ...

Filtering the JSON data shown according to an array in Angular 7

After receiving JSON data from the backend using a service, I am displaying it through a loop in main.html. There is an array that contains the values of a column being displayed. For example, if the array looks like this: colors=[red,blue], then only reco ...

Fetching an item from Local Storage in Angular 8

In my local storage, I have some data stored in a unique format. When I try to retrieve the data using localStorage.getItem('id');, it returns undefined. The issue lies in the way the data is stored within localStorage - it's structured as a ...

What type does the function in typescript return?

Could someone help me with this code snippet? export const getColor = (color: string): string => colors[color] || colors.white; I'm encountering a warning line under colors[color] || colors.white stating that it is an "Unsafe return of an any typ ...

Creating an Array in TypeScript

Is it possible to declare a global array in Typescript so that it can be accessed using "this" from any part of the code? In javascript, I would typically declare it as "var anArray=[]". What is the equivalent way of doing this in Typescript? Using anArra ...

Enhance the functionality of Spartacus CMS component with a new extension library

I am currently working on developing an Angular library that enhances the functionality of the product-images component within the product details page of Spartacus. Upon customizing a CMS component, I have realized the necessity to include code lines sim ...

What is the best way to utilize the parameter of a generic static method within a generic function?

My attempt at utilizing Omit<T, 'id'> within a generic function is shown below: class Model{ static create<T extends Model>(a: Omit<T, 'id'>): number{ return 0; } id = 0; title = 'test&apos ...