-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(forms): make form controls strictly typed #40772
Conversation
78b0d86
to
ccd266d
Compare
@MikeJerred |
Hi @MikeJerred, Thanks for creating this PR! Improvements to the Form types is an important topic and it's a part of Angular Roadmap. As @destus90 mentioned, there is a couple PRs with type improvements: #38406 (which updates types on existing classes) and #38906 (which adds a layer on top of existing APIs). There is no final decision on the approach yet and there is also a potential of (partially) combining the mentioned PRs (and may be also the changes from this PR too!). Even though there are other open PRs with type improvements, I'd like to keep your PR open as well and we'll review as a part of the effort to improve Forms typings. There is no ETA at this moment, so it may take some time for us to get back with review/comments. Thank you. |
Sounds good, here are some of the main differences from the existing PRs to make it easier to review:
const g = new FormGroup({
foo: new FormControl('value'),
bar: new FormControl(1)
});
g.removeControl('bar');
// g.value is now { foo: 'value' }
// but the type of g.value is { foo: 'value', bar: 1 }
|
This commit adds type arguments to all form controls and validators. BREAKING CHANGE: some methods called on `FormControl`, `FormArray`, or `FormGroup` may now cause a typescript error. Calling methods such as `FormControl.setValue` with a parameter of a different type to what was passed in the constructor of the control will cause a type error. Migration is possible by changing all instances of `FormControl` to `FormControl<any>`, `FormArray` to `FormArray<any>`, and `FormGroup` to `FormGroup<any>`. Closes angular#13721
2c0e845
to
c2c5c7d
Compare
@MikeJerred I like the idea of |
Thank you for this great PR. In particular, your control-types approach is instructive, and I am still referencing your typings for Validator. I am closing this PR because it is obviated by #43834. Thanks for the groundwork, and we will acknowledge your contribution in the changelog when Typed Forms lands. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Closes #13721
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #13721
What is the new behavior?
Type information is now available on all form controls and validators.
There is an additional control called
FormSection
which is very similar toFormGroup
except that controls cannot be added or removed at runtime, so it can contain different types of control and still have strong type information. I have not implemented an equivalent forFormArray
, but this could be done if there is a use case (perhaps call itFormTuple
?).Types should for the most part be inferred automatically by typescript when constructing the control (or using the
FormBuilder
).Examples:
Does this PR introduce a breaking change?
All places where a
FormControl
,FormArray
, orFormGroup
are constructed must be replaced withFormControl<any>
,FormArray<any>
orFormGroup<any>
respectively to ensure that existing code will not cause typescript errors, .e.g:Alternatively, the controls in this PR could be renamed to something else (e.g.
TypedFormControl
) and then haveFormControl
be an alias forTypedFormControl<any>
, in which case this PR would not introduce a breaking change.Other information