You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So after some experiments with creating such type I had a nice idea I even posted on an angular issue (angular/angular#46864 (comment)) concerning model to form model.
The idea allows to do:
This is not perfect but the best I could think of and gives in most cases (excluding discriminated union groups) a fully typed and scalable solution.
Part 2 - NoInferFormBuilder
Once I had the type from above and started working with it I encountered multiple issues with the form builder types and generally with typescript and forms. The solution I came up with is a new alternative to form builder using the NoInfer typescript type. I also posted a suggestion in the angular repo for it (angular/angular#58001).
The main idea is to be a drop in replacement for form builder when you are building a form with an already known form type. In that case you want typescript to infer only from the return type.
There are of course caveats like missing discriminate union case inference - but those are better than the shortcomings of not having the NoInfer builder.
Conclusion
As I don't know if it will be accepted by the angular team - I think here is a good place to add it for anyone that wants it as it makes building forms much much easier in cases you have an interface already.
Even in cases you don't have an interface - I find it easier to build my form type using regular typescript interfaces and then build the form from it as the auto inference of form types isn't always easy to use.
The suggestion is built from two parts:
Part 1 - Model to FormModel type:
So after some experiments with creating such type I had a nice idea I even posted on an angular issue (angular/angular#46864 (comment)) concerning model to form model.
The idea allows to do:
This is not perfect but the best I could think of and gives in most cases (excluding discriminated union groups) a fully typed and scalable solution.
Part 2 - NoInferFormBuilder
Once I had the type from above and started working with it I encountered multiple issues with the form builder types and generally with typescript and forms. The solution I came up with is a new alternative to form builder using the
NoInfer
typescript type. I also posted a suggestion in the angular repo for it (angular/angular#58001).The main idea is to be a drop in replacement for form builder when you are building a form with an already known form type. In that case you want typescript to infer only from the return type.
There are of course caveats like missing discriminate union case inference - but those are better than the shortcomings of not having the NoInfer builder.
Conclusion
As I don't know if it will be accepted by the angular team - I think here is a good place to add it for anyone that wants it as it makes building forms much much easier in cases you have an interface already.
Even in cases you don't have an interface - I find it easier to build my form type using regular typescript interfaces and then build the form from it as the auto inference of form types isn't always easy to use.
The implementation isn't complete but here is a stackblitz showing usage of both parts and a comparison between
FormBuilder
andNoInferFormBuilder
: https://stackblitz.com/edit/stackblitz-starters-me8z6o?file=src%2Fmain.tsThe text was updated successfully, but these errors were encountered: