fix(forms): Fix typing on FormRecord. (#59993)

Priori to this change, `ɵRawValue` of a `FormRecord` returned a `Partial`. This commit fixes it.

fixes #59985

PR Close #59993
This commit is contained in:
Matthieu Riegler
2025-02-18 12:42:48 +01:00
committed by Jessica Janiuk
parent da1426b3fe
commit ec1e4c3d94
3 changed files with 41 additions and 2 deletions
+1 -1
View File
@@ -556,7 +556,7 @@ export interface FormRecord<TControl> {
emitEvent?: boolean;
}): void;
setValue(value: {
[key: string]: ɵValue<TControl>;
[key: string]: ɵRawValue<TControl>;
}, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
+1 -1
View File
@@ -769,7 +769,7 @@ export interface FormRecord<TControl> {
* See `FormGroup#setValue` for additional information.
*/
setValue(
value: {[key: string]: ɵValue<TControl>},
value: {[key: string]: ɵRawValue<TControl>},
options?: {
onlySelf?: boolean;
emitEvent?: boolean;
@@ -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<FormGroup<{foo: FormControl<number>; bar: FormControl<number>}>>({
group1: new FormGroup({
foo: new FormControl(42, {nonNullable: true}),
bar: new FormControl(42, {nonNullable: true}),
}),
});
type ValueParam = Parameters<typeof fr.setValue>[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<typeof fr>;
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', () => {