Skip to content

Commit ec1e4c3

Browse files
JeanMechethePunderWoman
authored andcommitted
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
1 parent da1426b commit ec1e4c3

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

goldens/public-api/forms/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ export interface FormRecord<TControl> {
556556
emitEvent?: boolean;
557557
}): void;
558558
setValue(value: {
559-
[key: string]: ɵValue<TControl>;
559+
[key: string]: ɵRawValue<TControl>;
560560
}, options?: {
561561
onlySelf?: boolean;
562562
emitEvent?: boolean;

packages/forms/src/model/form_group.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export interface FormRecord<TControl> {
769769
* See `FormGroup#setValue` for additional information.
770770
*/
771771
setValue(
772-
value: {[key: string]: ɵValue<TControl>},
772+
value: {[key: string]: ɵRawValue<TControl>},
773773
options?: {
774774
onlySelf?: boolean;
775775
emitEvent?: boolean;

packages/forms/test/typed_integration_spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// These tests mainly check the types of strongly typed form controls, which is generally enforced
1010
// at compile time.
1111

12+
import {ɵRawValue} from '@angular/forms';
1213
import {FormBuilder, NonNullableFormBuilder, UntypedFormBuilder} from '../src/form_builder';
1314
import {
1415
AbstractControl,
@@ -728,6 +729,44 @@ describe('Typed Class', () => {
728729
c.reset({c: 42, d: 0});
729730
c.removeControl('c');
730731
});
732+
733+
it('should only accept non-partial values', () => {
734+
const fr = new FormRecord<FormGroup<{foo: FormControl<number>; bar: FormControl<number>}>>({
735+
group1: new FormGroup({
736+
foo: new FormControl(42, {nonNullable: true}),
737+
bar: new FormControl(42, {nonNullable: true}),
738+
}),
739+
});
740+
741+
type ValueParam = Parameters<typeof fr.setValue>[0];
742+
743+
// This should error if the typing allows partial values
744+
const value: ValueParam = {
745+
// @ts-expect-error
746+
group1: {
747+
foo: 42,
748+
// bar value is missing
749+
},
750+
};
751+
752+
type RecordRawValue = ɵRawValue<typeof fr>;
753+
const rawValue: RecordRawValue = {
754+
// @ts-expect-error
755+
group1: {
756+
foo: 42,
757+
// bar value is missing
758+
},
759+
};
760+
761+
expect(() =>
762+
fr.setValue({
763+
// @ts-expect-error
764+
group1: {
765+
foo: 42,
766+
},
767+
}),
768+
).toThrowError(/NG01002: Must supply a value for form control/);
769+
});
731770
});
732771

733772
describe('FormArray', () => {

0 commit comments

Comments
 (0)