0

I have multiple forms in different components. I am using formBuilder to create my formGroups for typed forms. When I am passing the created formGroups to a child component through @Input, I don't see the type assertion passed. Is there a way to do this to know the type of a formControl.

Parent Component:

public form1 = this.formBuilder.group({ name: ["", Vaidators.required] enabled: [false, aidators.required] }); public form2= this.formBuilder.group({ date: ["", Vaidators.required] }); public form3 = this.formBuilder.group({ data: ["", Vaidators.required], recurring: [true, Vaidators.required] }); public form = this.formBuilder.group({ form1: this.form1, form2: this.form2, form3: this.form3, });
<child1 [form1]="form1"></child1> <child2 [form2]="form2"></child2> <child3 [form3]="form3"></child3>

Child Component:

@Input() form1: FormGroup;
<form [formGroup]="from1"> <input type="text" formControlName="name" /> </form>

I know that I can create a Interface for each formGroup and use it in the child components. But, I would like to know if there is another way to do this.

interface form1 { name: FormControl<string>; } form: FormGroup<form1> = new FormGroup({ name: new FormControl("") })

1 Answer 1

0

Currently, there is no other way to enforce type assertion in Angular, other than creating interfaces for each FormGroup. This means that declaring your input to be of type just FormGroup makes it default to being of type FormGroup<any> - that is the reason why your IDE/compiler cannot infer the type of the form you pass.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.