Typed Forms: The RFC Is Here

Angular IT Development

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.

Long Journeys, Tricky Problems

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:

https://medium.com/media/b04ee5eb723e8755f4013acfe8c97d74/href

This is just the tip of the iceberg: the Forms API is very large, and there are many surfaces that are similarly untyped.

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.

This is just one example, and many other icebergs lurk beneath the surface. How can we type these in a backwards-compatible way? We’ve studied these issues, and collected our proposed fixes in the RFC.

What’s In the RFC?

The RFC serves a few purposes simultaneously.

First, it collects our proposals about the crucial design questions. The RFC presents each design point together with explanations and examples, in a way that should be concise and easily understood.

Second, it provides a working prototype. We’d love for you to try our prototype, especially with your own app, and let us know how it goes. This is one of the most important ways you can help us when reviewing the RFC.

Finally, we’d love to gather feedback about the proposal. We have included some especially interesting questions, but don’t feel limited to just those topics.

The Road Ahead

Typed Forms provides valuable benefits on its own, but the feature will also enable us to evolve the Forms package in other ways.

One next step is improvements in template type checking. Now that we can check types for forms classes in TypeScript, we’d love to bring those improvements into forms templates as well. This will require upgrades to the template type checker, and should nicely broaden the impact of the new types.

There are some other potential beneficiaries as well: We’d love to clean up the way state change events are handled. Having strong types will allow us to provide a consistent and correct experience everywhere, from ControlValueAccessor to observables on AbstractControl.

We are incredibly excited about this new direction for Forms, both the types themselves and the many improvements to come.

Typed Forms: The RFC Is Here was originally published in Angular Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.