Visitar URL original
fix(forms): allow FormBuilder.group(...) to accept optional fields. by dylhunn · Pull Request #46253 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20220610043133/https://github.com/angular/angular/pull/46253
Skip to content
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
wants to merge 1 commit into from

Conversation

dylhunn
Copy link
Contributor

@dylhunn dylhunn commented Jun 3, 2022

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.

Because the optional {address?: FormControl<string|null>} implicitly makes the RHS have type FormControl<string|null>|undefined, the relevant condition is not satisfied. 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).

Google-internal bug: b/234856858

@dylhunn dylhunn added type: bug/fix action: review comp: forms target: patch action: presubmit cross-cutting: types P1 forms: strictly typed labels Jun 3, 2022
@dylhunn dylhunn requested a review from AndrewKushnir Jun 3, 2022
@ngbot ngbot bot added this to the Backlog milestone Jun 3, 2022
@ngbot ngbot bot added this to the Backlog milestone Jun 3, 2022
@dylhunn dylhunn force-pushed the fb-group-optional branch 2 times, most recently from 1fc488c to 91e6fe2 Compare Jun 3, 2022
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

LGTM 👍 Just a quick comment on adding more docs (which can also be done in a followup PR).

packages/forms/src/form_builder.ts Outdated Show resolved Hide resolved
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).
@dylhunn
Copy link
Contributor Author

@dylhunn dylhunn commented Jun 4, 2022

@dylhunn dylhunn added action: merge and removed action: presubmit labels Jun 4, 2022
@alxhub
Copy link
Contributor

@alxhub alxhub commented Jun 6, 2022

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 comp: forms cross-cutting: types forms: strictly typed P1 target: patch type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants