Skip to content

Commit f4f1bcc

Browse files
lazarljubenovicjosephperrott
authored andcommitted
fix(forms): include null in .parent of abstract control (angular#32671)
It's perfectly valid for an abstract control not to have a defined parent; yet previously the types were asserting that AbstractControl#parent is not a null value. This changes correctly reflects the run-time behavior through the types. BREAKING CHANGE: Type of AbstractFormControl.parent now includes null `null` is now included in the types of .parent. If you don't already have a check for this case, the TypeScript compiler might compain. A v11 migration exists which adds the not-null assertion operator where necessary. In an unlikely case your code was testing the parnet against undefined with sitrct equality, you'll need to change this to `=== null` instead, since the parent is not explicily initialized with `null` instead of being left `undefined`. Fixes angular#16999 PR Close angular#32671
1 parent 4beaddc commit f4f1bcc

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

goldens/public-api/forms/forms.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export declare abstract class AbstractControl {
66
get enabled(): boolean;
77
readonly errors: ValidationErrors | null;
88
get invalid(): boolean;
9-
get parent(): FormGroup | FormArray;
9+
get parent(): FormGroup | FormArray | null;
1010
get pending(): boolean;
1111
readonly pristine: boolean;
1212
get root(): AbstractControl;

packages/forms/src/model.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ export abstract class AbstractControl {
173173
// TODO(issue/24571): remove '!'.
174174
_updateOn!: FormHooks;
175175

176-
// TODO(issue/24571): remove '!'.
177-
private _parent!: FormGroup|FormArray;
176+
private _parent: FormGroup|FormArray|null = null;
178177
private _asyncValidationSubscription: any;
179178

180179
/**
@@ -267,7 +266,7 @@ export abstract class AbstractControl {
267266
/**
268267
* The parent control.
269268
*/
270-
get parent(): FormGroup|FormArray {
269+
get parent(): FormGroup|FormArray|null {
271270
return this._parent;
272271
}
273272

@@ -1018,7 +1017,7 @@ export abstract class AbstractControl {
10181017
*/
10191018
private _parentMarkedDirty(onlySelf?: boolean): boolean {
10201019
const parentDirty = this._parent && this._parent.dirty;
1021-
return !onlySelf && parentDirty && !this._parent._anyControlsDirty();
1020+
return !onlySelf && !!parentDirty && !this._parent!._anyControlsDirty();
10221021
}
10231022
}
10241023

0 commit comments

Comments
 (0)