Typed Forms: The RFC Is Here

submited by
Style Pass
2022-01-13 20:00:05

For five years, strong types in the Forms package have been a top feature request. Issue (#13721) is the most popular issue of all time on the Angular repo. Many contributors have attempted to implement it over the years.

We’re proud to announce the Typed Forms Request for Comment. This is the first big step toward adding this long-sought feature to Angular.

With strong types, the values inside of form controls, groups, and arrays can be more safely accessed. Many users access a control type the first time they interact with Forms, and wonder why it has a value of any:

Additionally, the API itself presents a number of challenges, which are explored in depth in the RFC. These challenges include details of how controls are reset, partial FormGroup values, disabled controls, and much more.

Although the full details are in the RFC, exploring one of the trickiest parts gives a flavor: resetting controls. The Forms package currently behaves in a very unsafe way: controls reset to null, which can violate the expected value type. For instance, a FormControl<string> might be intended to have a string value, but calling reset will cause it to become null, violating that type contract. To solve this particular issue, we introduced a new option when constructing a FormControl called initialValueIsDefault. Setting this flag causes the control to reset to its initial value, rather than null.

Leave a Comment