DEV Community

Jonathan Goossens
Jonathan Goossens

Posted on

Need help with strictly typed forms

Hi,

I am trying to make use of Angular's typed forms, but I am running into some issues regarding the typing. I created this small stackblitz example to demonstrate what I'm trying to achieve. When I define a typed form inside my building.component.ts, it works completely fine. However, due to the size of the forms I'm using, I'm attempting to build the form in a separate function (in a separate file) to keep my code cleaner. However, whenever I do this, it loses the typing. Does anyone here have any experience with this issue or any knowledge that could help me achieve my goal?

Thanks in advance!

Top comments (1)

Collapse
 
dhutaryan profile image
Dzmitry Hutaryan

Why do you wanna init form in a separate file? I've met this solution and it worked bad.
About your question: you need to remove returned type for initBuildingForm function or specify it in full. FormGroup is a generic.

FormGroup<{
  buildingType: FormControl<number | null>;
  premiumExactAmount: FormControl<number | null>;
  premiumEstimation: FormControl<number | null>;
  buildingStatus: FormControl<number | null>;
  startDate: FormControl<Date | null>;
}>
Enter fullscreen mode Exit fullscreen mode