Skip to content

Commit

Permalink
fix(forms): FormBuilder.group return right type with shorthand parame…
Browse files Browse the repository at this point in the history
…ters.

Extract AbstractControlOptions from type when used in shorthand parameters on FormBuilder.group

Fixes 48073
  • Loading branch information
JeanMeche committed Nov 16, 2022
1 parent fc47141 commit 686dbb9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/forms/src/form_builder.ts
Expand Up @@ -82,7 +82,7 @@ export type ɵElement<T, N extends null> =
// FormControlState object container, which produces a nullable control.
[T] extends [FormControlState<infer U>] ? FormControl<U|N> :
// A ControlConfig tuple, which produces a nullable control.
[T] extends [PermissiveControlConfig<infer U>] ? FormControl<Exclude<U, ValidatorConfig>|N> :
[T] extends [PermissiveControlConfig<infer U>] ? FormControl<Exclude<U, ValidatorConfig| AbstractControlOptions>|N> :
FormControl<T|N>;

// clang-format on
Expand Down
12 changes: 11 additions & 1 deletion packages/forms/test/form_builder_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/
import {Component} from '@angular/core';
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
import {FormBuilder, NonNullableFormBuilder, ReactiveFormsModule, UntypedFormBuilder, Validators} from '@angular/forms';
import {FormBuilder, NonNullableFormBuilder, ReactiveFormsModule, UntypedFormBuilder, Validators,} from '@angular/forms';
import {of} from 'rxjs';

(function() {
Expand Down Expand Up @@ -189,6 +189,16 @@ describe('Form Builder', () => {
expect(g.asyncValidator).toBe(null);
});

it('should create groups with shorthand parameters and with right typings', () => {
const form = b.group({
shorthand: [3, Validators.required],
shorthand2: [5, {validators: Validators.required}],
});

expect((form.get('shorthand')?.value)).toEqual(3);
expect(((form.get('shorthand2')!.value ?? 0) + 0)).toEqual(5);
});

it('should create control arrays', () => {
const c = b.control('three');
const e = b.control(null);
Expand Down

0 comments on commit 686dbb9

Please sign in to comment.