Skip to content

feat(forms): add strict reactive forms api#38906

Closed
MBuchalik wants to merge 1 commit intoangular:masterfrom
MBuchalik:strict-reactive-forms
Closed

feat(forms): add strict reactive forms api#38906
MBuchalik wants to merge 1 commit intoangular:masterfrom
MBuchalik:strict-reactive-forms

Conversation

@MBuchalik
Copy link

@MBuchalik MBuchalik commented Sep 19, 2020

Add StrictFormArray, StrictFormControl and StrictFormGroup, allowing the developer to specify a "form model".
This makes the structure of forms strictly typed,
which prevents errors and leads to better support for code auto completion.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Reactive Forms are currently not strongly typed. This leads to a suboptimal developer experience and does not catch common mistakes (e.g. typos).

Issue Number: #13721

What is the new behavior?

There are (/have been) multiple attempts to make FormArray, FormControl and FormGroup type safe. My proposal is different compared to the already existing ones: Instead of directly making these three classes type safe, I have introduced three new classes: StrictFormArray, StrictFormControl and StrictFormGroup (inheriting from their non-strict variants).
Developers have to define a separate "form model" that describes the structure of the form. Once this has been done, you get lots of benefits, including support for code suggestions/autocompletion, warnings when accessing non-existing controls, etc.

Benefits compared to other approaches:

  • Guaranteed not to be a breaking change
  • Developers can gradually migrate to this optional API
  • StrictFormControls can still hold objects and not only primitive values

Drawbacks:

  • Developers need to define a separate form model, so they have to type more (pun intended)

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I was running the tests on a pretty slow computer, so many just timed out (FirefoxHeadless failed 2 times (timeout). Giving up.) Hoping for CI to provide the results.

Since my PR basically only includes a new model, I did not know if I had to create new tests. If so, I would be happy about a small explanation/example on how these tests should look like. My thoughts: I am literally only forwarding calls to the super classes, so there isn't much that could break.

(And one small thing: Most of the examples from the docs are based on ones of the respective non-strict version.)

Add StrictFormArray, StrictFormControl and StrictFormGroup, allowing the developer to specify a "form model".
This makes the structure of forms strictly typed,
which prevents errors and leads to better support for code auto completion.
@pullapprove pullapprove bot requested a review from AndrewKushnir September 19, 2020 12:36
@sonukapoor sonukapoor added area: forms forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. labels Sep 19, 2020
@ngbot ngbot bot modified the milestone: needsTriage Sep 19, 2020
@AndrewKushnir
Copy link
Contributor

@MBuchalik thanks for creating this PR.

Improving types in Angular forms is on our Roadmap. We are looking into possible ways on how we can update existing classes to improve typings (see #38406). If we come across major blockers (and/or unclear upgrade path) during the research, we may need to reconsider the approach. Let's keep this PR open, we'll get back to it once the research in #38406 is completed (no ETA at this moment).

Thank you.

@MBuchalik
Copy link
Author

Sounds good 👍

@dobromyslov
Copy link

1 month has passed since last commit in #38406. Any news? Need any help?

@dylhunn
Copy link
Contributor

dylhunn commented Nov 4, 2021

Thank you for this PR, and I have read it in detail over the last ~2 months while working on Typed Forms. This was quite valuable in the design phase, but we ultimately decided to add types to the existing API later rather than wrapping it with a new layer. I am closing this PR in favor of the in-progress #43834. You can find in-depth justification of decision to extend rather than wrap on that PR's design doc.

I'll be sure to acknowledge your contribution with special thanks in the changelog when the above PR lands.

@dylhunn dylhunn closed this Nov 4, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: forms cla: yes forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants