From ec1e4c3d9430f5ea4380252098d2b4b71d8a950f Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Tue, 18 Feb 2025 12:42:48 +0100 Subject: [PATCH] fix(forms): Fix typing on `FormRecord`. (#59993) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Priori to this change, `ɵRawValue` of a `FormRecord` returned a `Partial`. This commit fixes it. fixes #59985 PR Close #59993 --- goldens/public-api/forms/index.api.md | 2 +- packages/forms/src/model/form_group.ts | 2 +- packages/forms/test/typed_integration_spec.ts | 39 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/goldens/public-api/forms/index.api.md b/goldens/public-api/forms/index.api.md index 4c54cd86d0f..52d149b932f 100644 --- a/goldens/public-api/forms/index.api.md +++ b/goldens/public-api/forms/index.api.md @@ -556,7 +556,7 @@ export interface FormRecord { emitEvent?: boolean; }): void; setValue(value: { - [key: string]: ɵValue; + [key: string]: ɵRawValue; }, options?: { onlySelf?: boolean; emitEvent?: boolean; diff --git a/packages/forms/src/model/form_group.ts b/packages/forms/src/model/form_group.ts index 9841893df00..2d6e1885c42 100644 --- a/packages/forms/src/model/form_group.ts +++ b/packages/forms/src/model/form_group.ts @@ -769,7 +769,7 @@ export interface FormRecord { * See `FormGroup#setValue` for additional information. */ setValue( - value: {[key: string]: ɵValue}, + value: {[key: string]: ɵRawValue}, options?: { onlySelf?: boolean; emitEvent?: boolean; diff --git a/packages/forms/test/typed_integration_spec.ts b/packages/forms/test/typed_integration_spec.ts index c66f220a841..fc515b4cb49 100644 --- a/packages/forms/test/typed_integration_spec.ts +++ b/packages/forms/test/typed_integration_spec.ts @@ -9,6 +9,7 @@ // These tests mainly check the types of strongly typed form controls, which is generally enforced // at compile time. +import {ɵRawValue} from '@angular/forms'; import {FormBuilder, NonNullableFormBuilder, UntypedFormBuilder} from '../src/form_builder'; import { AbstractControl, @@ -728,6 +729,44 @@ describe('Typed Class', () => { c.reset({c: 42, d: 0}); c.removeControl('c'); }); + + it('should only accept non-partial values', () => { + const fr = new FormRecord; bar: FormControl}>>({ + group1: new FormGroup({ + foo: new FormControl(42, {nonNullable: true}), + bar: new FormControl(42, {nonNullable: true}), + }), + }); + + type ValueParam = Parameters[0]; + + // This should error if the typing allows partial values + const value: ValueParam = { + // @ts-expect-error + group1: { + foo: 42, + // bar value is missing + }, + }; + + type RecordRawValue = ɵRawValue; + const rawValue: RecordRawValue = { + // @ts-expect-error + group1: { + foo: 42, + // bar value is missing + }, + }; + + expect(() => + fr.setValue({ + // @ts-expect-error + group1: { + foo: 42, + }, + }), + ).toThrowError(/NG01002: Must supply a value for form control/); + }); }); describe('FormArray', () => {