Collection of personalized forms where the parent is a FormGroup

One scenario I'm working on involves creating multiple custom formgroup classes that have FormGroup as their parent class, structured like this:

export class CustomFormGroup1 extends FormGroup {
  //custom properties for this FormGroup
  
  constructor() {
    super(
      {
      // initializing CustomFormGroup1's form properties
      }
    );
  }
}

export class CustomFormGroup2 extends FormGroup {
  //custom properties for this FormGroup
  
  constructor() {
    super(
      {
      // initializing CustomFormGroup2's form properties
      }
    );
  }
}

// and similar classes with unique characteristics

My goal is to place these classes in an array where the type is FormGroup, as shown here:

formList: FormGroup[] = [CustomFormGroup1, CustomFormGroup2, ...etc]

However, when attempting this, I encounter the following error message:

TS2740: Type 'typeof ResourceTypeForm' is missing the following properties from type 'FormGroup': controls, registerControl, addControl, removeControl, and 57 more. 

I've searched for solutions and came across similar inquiries such as: Type 'AbstractControl' is missing the following properties from type 'FormGroup': controls, registerControl, addControl, removeControl, and 3 more or TypeScript type of array with common Classes that inherit from same Class but none specifically addressing my particular issue.

In essence, my question is: Is there a way to define an array with objects sharing the same parent class?

While I could potentially implement all the methods for FormGroup within each custom form group class, I am hoping for a solution that avoids that redundancy.

I have also attempted defining it like this:

formArray: (FormGroup|CustomFormGroup1|CustomFormGroup2|..etc)[] = [CustomFormGroup1, CustomFormGroup2, ...etc];

Yet, I receive the same warning mentioned earlier.

Thank you for any insights you may provide.

Appreciate your assistance.

Answer №1

If you want to include types that inherit from FormGroup (or the FormGroup itself) in the array, you can achieve this by using the following syntax:

formList: (typeof FormGroup)[] = [CustomFormGroup1, CustomFormGroup2, ...and so on]

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

Select the implied type from a resolved Promise type in Typescript

I am working with a function called getStaticProps in Next.js that returns a Promise, resolving to an Object with the following structure: type StaticProps<P> = { props: P; revalidate?: number | boolean; } To generically "unwrap" the type o ...

What is the best way to dynamically load content on a page when the URL changes within the same angular component?

I'm attempting to have a page load specific content when the URL changes, all within the same component. myComponent.ts ngOnInit(){ this.router.events.subscribe(res=> { if (this.router.url.split('?')[0].split("/").pop() === &ap ...

What is the best way to export Firebase admin in Typescript?

Struggling to export Firebase admin for access in my modules while using Typescript. Upon importing it into my modules, the properties of admin are not recognized. admin.ts import * as admin from "firebase-admin"; admin.initializeApp(); module. ...

Encountering an Issue with Angular 9's HttpInterceptor: Unraveling the Mystery of

For my Angular 9 application, I have implemented an HttpInterceptor as shown below: export class AppHttpInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { ...

Error: inability to execute performanceMeasurement.startMeasurement due to its absence in the function definition

An error occurred when attempting to login using @azure/msal-react in Next 13. Upon checking the error log, it was found that the error originated from the core library @azure/msal-react. Even after trying with a login popup, the error persisted. In my co ...

A guide for implementing fast-json-patch in your Angular 2 projects

Looking to incorporate the "fast-json-patch" library into my Angular 2 project. This library can be found at https://github.com/Starcounter-Jack/JSON-Patch. I attempted to include the following code: 'fast-json-patch': 'vendor/fast-json-pa ...

Comparing the properties of objects in two arrays can be done most effectively by utilizing the most efficient method available

In Angular2, I am looking for a more efficient way to check if an object's property in one array matches a property in another array and return the value. Is there a method similar to using .contains in Swift? doSomething(){ for (let element1 of ...

The element body-component is not recognized within the Angular quickstart framework

My attempt at a simple code isn't working as expected. I have some knowledge of angular, but I'm getting frustrated with this issue. ;( Here is the structure of my code: <body-component></body-component> In my body.component.html f ...

Updating a one-to-one relationship in TypeORM with Node.js and TypeScript can be achieved by following these steps

I am working with two entities, one is called Filter and the other is Dataset. They have a one-to-one relationship. I need help in updating the Filter entity based on Dataset using Repository with promises. The code is written in a file named node.ts. Th ...

Using Angular: How to access a component variable within a CSS file

I'm having issues with my css file and attempting to achieve the following: .login-wrapper { background-image: url({{imagePath}}); } Below is my component code: export class LoginComponent implements OnInit { imagePath: any = 'assets/discord ...

What event type should be used for handling checkbox input events in Angular?

What is the appropriate type for the event parameter? I've tried using InputEvent and HTMLInputElement, but neither seems to be working. changed(event) { //<---- event?? console.log({ checked: event.target.checked }); } Here's the com ...

Is it possible to implement a different termination condition when using *ngFor in Angular 2?

After countless hours of searching on Google, I have yet to discover a method for implementing an alternative stop condition for loops created with the *ngFor directive. By default, *ngFor loops end with this condition: index < array.length. Is there a ...

What is the reason for TypeScript not providing warnings for unrealistic conditions involving 'typeof' and 'in'?

The recent updates in version 4.9 highlighted the enhanced narrowing with 'in'. Intrigued by this, I decided to experiment with their example in a coding playground. Surprisingly, I discovered that seemingly impossible conditions involving typeof ...

The PWA application is experiencing crashes on Safari following the recent IOS 17 update

Recently, I encountered an issue with my Angular app functioning as a PWA on my iPhone. Everything was working smoothly until the latest iOS 17 update. Now, the app crashes frequently and even when I clear the cache on Safari, it only works for a few min ...

Patience is key as you await the completion of an API call in Angular 14

Within my Angular 14 application, I am faced with a scenario where I need to make two API calls and then combine the results using "combineLatest" from rxjs. The combined data is then assigned to a variable that I must use in a separate function. How can I ...

Is it possible to leverage lifecycle hooks within Angular Guards?

I have been working with Angular and in my project, the action for loading data on first page load is dispatched from a guard. I recently made modifications to the guard to allow access to the store. I subscribe to the store in the constructor to access th ...

What is the best way to upload a file using a relative path in Playwright with TypeScript?

Trying to figure out how to upload a file in a TypeScript test using Playwright. const fileWithPath = './abc.jpg'; const [fileChooser] = await Promise.all([ page.waitForEvent('filechooser'), page.getByRole('button' ...

What is causing ESLint to point out the issue with the @inheritdoc tag?

My code in ESLint is throwing an error about a missing JSDoc return declaration, even though I have included an @inheritdoc tag there: https://i.sstatic.net/QGxQh.png Here is the section from the interface I am inheriting from: export interface L2BlockSo ...

Steps to modify the background color of an IconButton upon clicking in Material UI

After clicking on the IconButton, the background color changes to an oval shape. I now need to modify the background color when it is clicked. CSS The CSS code for the IconButton changes the background color upon hover. I require a similar effect for the ...

What could be the reason for Angular to merge the element at index 0 of an array into a subarray instead of doing

After setting up the Array in my oninit function, I encountered an issue where one part of the array was functioning as intended while the other returned an error. this.tests = [{ status: 0, testresults: [{ name: 'test ...