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
fix(forms): allow FormBuilder.group(...) to accept optional fields. #46253
Closed
+31
−8
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1fc488c
to
91e6fe2
Compare
AndrewKushnir
approved these changes
Jun 3, 2022
LGTM
Consider the case in which `FormBuilder` is used to construct a group with an optional field:
```
const controls = { name: fb.control('') };
const foo: FormGroup<{
name: FormControl<string | null>;
address?: FormControl<string | null>;
}> = fb.group<{
name: FormControl<string | null>;
address?: FormControl<string | null>;
}>(controls);
```
Today, with fully strict TypeScript settings, the above will not compile:
```
Types of property 'controls' are incompatible.
Type '{ name: FormControl<string | null>; address?: FormControl<FormGroup<SubFormControls> | null | undefined> | undefined; }' is not assignable to type '{ name: FormControl<string | null>; address?: FormGroup<SubFormControls> | undefined; }'.
```
Notice that the `fb.group(...)` is calculating the following type for address: `address?: FormControl<FormGroup<string|null>`. This is clearly wrong -- an extraneous `FormControl` has been added!
This is coming from the calculation of the result type of `fb.group(...)`. In the type definition, if we cannot detect the outer control type, [we assume it's just an unwrapped value, and automatically wrap it in `FormControl`](https://github.com/angular/angular/blob/14.0.0/packages/forms/src/form_builder.ts#L66).
Because the optional `{address?: FormControl<string|null>}` implicitly makes the RHS have type `FormControl<string|null>|undefined`, [the relevant condition is not satisfied](https://github.com/angular/angular/blob/14.0.0/packages/forms/src/form_builder.ts#L55). In particular, the condition expects just `FormGroup<T>`, not `FormGroup<T>|undefined`. So we assume `T` is a value type, and it gets wrapped with `FormControl`.
The solution is to add the cases where `undefined` is included in the union type when detecting which control `T` is (if any).
|
Running a g3 presubmit: http://test/OCL:452873359:BASE:452873371:1654302013460:85031e43 |
|
This PR was merged into the repository by commit f18e173. |
alxhub
pushed a commit
that referenced
this issue
Jun 6, 2022
…46253) Consider the case in which `FormBuilder` is used to construct a group with an optional field: ``` const controls = { name: fb.control('') }; const foo: FormGroup<{ name: FormControl<string | null>; address?: FormControl<string | null>; }> = fb.group<{ name: FormControl<string | null>; address?: FormControl<string | null>; }>(controls); ``` Today, with fully strict TypeScript settings, the above will not compile: ``` Types of property 'controls' are incompatible. Type '{ name: FormControl<string | null>; address?: FormControl<FormGroup<SubFormControls> | null | undefined> | undefined; }' is not assignable to type '{ name: FormControl<string | null>; address?: FormGroup<SubFormControls> | undefined; }'. ``` Notice that the `fb.group(...)` is calculating the following type for address: `address?: FormControl<FormGroup<string|null>`. This is clearly wrong -- an extraneous `FormControl` has been added! This is coming from the calculation of the result type of `fb.group(...)`. In the type definition, if we cannot detect the outer control type, [we assume it's just an unwrapped value, and automatically wrap it in `FormControl`](https://github.com/angular/angular/blob/14.0.0/packages/forms/src/form_builder.ts#L66). Because the optional `{address?: FormControl<string|null>}` implicitly makes the RHS have type `FormControl<string|null>|undefined`, [the relevant condition is not satisfied](https://github.com/angular/angular/blob/14.0.0/packages/forms/src/form_builder.ts#L55). In particular, the condition expects just `FormGroup<T>`, not `FormGroup<T>|undefined`. So we assume `T` is a value type, and it gets wrapped with `FormControl`. The solution is to add the cases where `undefined` is included in the union type when detecting which control `T` is (if any). PR Close #46253
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
action: merge
PR author is ready for this to merge
comp: forms
cross-cutting: types
forms: strictly typed
P1
Urgent priority issue that should be resolved before the next release
target: patch
This PR is targeted for the next patch release
type: bug/fix
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.


Consider the case in which
FormBuilderis used to construct a group with an optional field:Today, with fully strict TypeScript settings, the above will not compile:
Notice that the
fb.group(...)is calculating the following type for address:address?: FormControl<FormGroup<string|null>. This is clearly wrong -- an extraneousFormControlhas been added!This is coming from the calculation of the result type of
fb.group(...). In the type definition, if we cannot detect the outer control type, we assume it's just an unwrapped value, and automatically wrap it inFormControl.Because the optional
{address?: FormControl<string|null>}implicitly makes the RHS have typeFormControl<string|null>|undefined, the relevant condition is not satisfied. In particular, the condition expects justFormGroup<T>, notFormGroup<T>|undefined. So we assumeTis a value type, and it gets wrapped withFormControl.The solution is to add the cases where
undefinedis included in the union type when detecting which controlTis (if any).Google-internal bug: b/234856858