mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
Jessica Janiuk
parent
da1426b3fe
commit
ec1e4c3d94
@@ -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;
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user