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): Prevent FormBuilder from distributing unions to control types #45942

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/forms/src/form_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ export type ControlConfig<T> = [T|FormControlState<T>, (ValidatorFn|(ValidatorFn
* The flag N, if not never, makes the resulting `FormControl` have N in its type.
*/
export type ɵElement<T, N extends null> =
T extends FormControl<infer U> ? FormControl<U> :
T extends FormGroup<infer U> ? FormGroup<U> :
T extends FormArray<infer U> ? FormArray<U> :
T extends AbstractControl<infer U> ? AbstractControl<U> :
T extends FormControlState<infer U> ? FormControl<U|N> :
T extends ControlConfig<infer U> ? FormControl<U|N> :
// The `extends` checks are wrapped in arrays in order to prevent TypeScript from applying type unions
// through the distributive conditional type. This is the officially recommended solution:
// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
[T] extends [FormControl<infer U>] ? FormControl<U> :
[T] extends [FormGroup<infer U>] ? FormGroup<U> :
[T] extends [FormArray<infer U>] ? FormArray<U> :
[T] extends [AbstractControl<infer U>] ? AbstractControl<U> :
[T] extends [FormControlState<infer U>] ? FormControl<U|N> :
[T] extends [ControlConfig<infer U>] ? FormControl<U|N> :
// ControlConfig can be too much for the compiler to infer in the wrapped case. This is
// not surprising, since it's practically death-by-polymorphism (e.g. the optional validators
// members that might be arrays). Watch for ControlConfigs that might fall through.
T extends Array<infer U|ValidatorFn|ValidatorFn[]|AsyncValidatorFn|AsyncValidatorFn[]> ? FormControl<U|N> :
[T] extends [Array<infer U|ValidatorFn|ValidatorFn[]|AsyncValidatorFn|AsyncValidatorFn[]>] ? FormControl<U|N> :
// Fallthough case: T is not a container type; use it directly as a value.
FormControl<T|N>;

Expand Down
12 changes: 12 additions & 0 deletions packages/forms/test/typed_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,18 @@ describe('Typed Class', () => {
expect(c.value).toEqual({foo: 'bar'});
});

it('without distributing union types', () => {
const c = fb.group({foo: 'bar' as string | number});
{
type ControlsType = {foo: FormControl<string|number>};
let t: ControlsType = c.controls;
let t1 = c.controls;
t1 = null as unknown as ControlsType;
}
let fc = c.controls.foo;
fc = new FormControl<string|number>('', {initialValueIsDefault: true});
});

describe('from objects with FormControls', () => {
it('from objects with builder FormGroups', () => {
const c = fb.group({foo: fb.group({baz: 'bar'})});
Expand Down