angular/packages
Dylan Hunn f18e1739b8 fix(forms): allow FormBuilder.group(...) to accept optional fields. (#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
2022-06-06 10:14:19 -07:00
..
animations feat(bazel): speed up dev-turnaround by bundling types only when packaging (#45405) 2022-04-21 11:09:39 -07:00
bazel perf(bazel): reduce input files for ng_package rollup and type bundle actions (#46187) 2022-06-02 13:43:22 -07:00
benchpress build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
common refactor(common): remove unused method in internal SubscriptionStrategy interface (#46128) 2022-06-02 13:42:38 -07:00
compiler refactor(compiler-cli): introduce onlyPublishPublicTypingsForNgModules (#45894) 2022-06-02 13:39:14 -07:00
compiler-cli refactor(compiler-cli): introduce onlyPublishPublicTypingsForNgModules (#45894) 2022-06-02 13:39:14 -07:00
core fix(forms): allow FormBuilder.group(...) to accept optional fields. (#46253) 2022-06-06 10:14:19 -07:00
docs refactor: fix various typos across different packages (#44523) 2022-01-07 18:11:10 +00:00
elements feat(core): drop support for Node.js 12 (#45286) 2022-03-08 12:05:03 -08:00
examples refactor(core): drop all .ngfactory and .ngsummary imports (#44957) 2022-02-07 15:31:49 -08:00
forms fix(forms): allow FormBuilder.group(...) to accept optional fields. (#46253) 2022-06-06 10:14:19 -07:00
language-service fix(language-service): Fix detection of Angular for v14+ projects (#45998) 2022-05-16 10:03:53 -07:00
localize build: lock file maintenance to avoid node module differences for babel types (#45967) 2022-05-20 14:18:09 -07:00
misc/angular-in-memory-web-api build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
platform-browser docs(core): mark the standalone APIs as Developer Preview (#46050) 2022-06-01 16:01:59 -07:00
platform-browser-dynamic feat(bazel): speed up dev-turnaround by bundling types only when packaging (#45405) 2022-04-21 11:09:39 -07:00
platform-server docs(core): mark the standalone APIs as Developer Preview (#46050) 2022-06-01 16:01:59 -07:00
private/testing test: remove Ivy/ViewEngine switch helpers and obsolete tests (#44120) 2021-11-24 19:42:39 +00:00
router test(router): illustrate canceling of intermediate results in canLoad (#46231) 2022-06-03 10:06:54 -07:00
service-worker feat(bazel): speed up dev-turnaround by bundling types only when packaging (#45405) 2022-04-21 11:09:39 -07:00
upgrade feat(upgrade): more closely align UpgradeModule#bootstrap() with angular.bootstrap() (#46214) 2022-06-03 10:16:05 -07:00
zone.js build: lock file maintenance (#46104) 2022-06-03 10:23:36 -07:00
BUILD.bazel build: Auto-gen tsec tsconfig files (#43440) 2021-09-17 17:17:34 +00:00
circular-deps-test.conf.js refactor: replace deprecated String.prototype.substr() (#45397) 2022-03-24 11:48:09 -07:00
empty.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
goog.d.ts refactor(core): replace references to goog.LOCALE with goog.getLocale() (#43089) 2021-08-10 10:35:26 -07:00
license-banner.txt build: bump license year (#44590) 2022-01-04 12:05:25 -08:00
README.md docs: add doc reference to npm package readme (#33911) 2019-11-20 14:46:23 -08:00
system.d.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
tsconfig-build.json build: enable useUnknownInCatchVariables (#44679) 2022-02-01 18:17:29 +00:00
tsconfig-legacy-saucelabs.json build: bundle spec files for saucelabs legacy job (#44281) 2021-11-30 11:56:04 -05:00
tsconfig-test.json build: switch devmode output to es2015 (#44505) 2022-01-05 23:20:20 +00:00
tsconfig-tsec-base.json build: update tsconfigs to use ES2020 as target and module (#43431) 2021-10-01 18:28:42 +00:00
tsconfig.json build: enable useUnknownInCatchVariables (#44679) 2022-02-01 18:17:29 +00:00
tsec-exemption.json build: Enable tsec checks for critical packages. (#43108) 2021-09-13 14:45:57 -07:00
types.d.ts build: update to jasmine 4.0 (#45558) 2022-04-11 16:25:28 +00:00

Angular

The sources for this package are in the main Angular repo. Please file issues and pull requests against that repo.

Usage information and reference details can be found in Angular documentation.

License: MIT