From f4f1bcc99711be277205dcd8ef6fc5a1873cb227 Mon Sep 17 00:00:00 2001 From: lazarljubenovic Date: Fri, 13 Sep 2019 17:17:18 +0200 Subject: [PATCH] fix(forms): include null in .parent of abstract control (#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 #16999 PR Close #32671 --- goldens/public-api/forms/forms.d.ts | 2 +- packages/forms/src/model.ts | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/goldens/public-api/forms/forms.d.ts b/goldens/public-api/forms/forms.d.ts index a2926bb425..e1d7c36544 100644 --- a/goldens/public-api/forms/forms.d.ts +++ b/goldens/public-api/forms/forms.d.ts @@ -6,7 +6,7 @@ export declare abstract class AbstractControl { get enabled(): boolean; readonly errors: ValidationErrors | null; get invalid(): boolean; - get parent(): FormGroup | FormArray; + get parent(): FormGroup | FormArray | null; get pending(): boolean; readonly pristine: boolean; get root(): AbstractControl; diff --git a/packages/forms/src/model.ts b/packages/forms/src/model.ts index f8c2558561..e3356e5726 100644 --- a/packages/forms/src/model.ts +++ b/packages/forms/src/model.ts @@ -173,8 +173,7 @@ export abstract class AbstractControl { // TODO(issue/24571): remove '!'. _updateOn!: FormHooks; - // TODO(issue/24571): remove '!'. - private _parent!: FormGroup|FormArray; + private _parent: FormGroup|FormArray|null = null; private _asyncValidationSubscription: any; /** @@ -267,7 +266,7 @@ export abstract class AbstractControl { /** * The parent control. */ - get parent(): FormGroup|FormArray { + get parent(): FormGroup|FormArray|null { return this._parent; } @@ -1018,7 +1017,7 @@ export abstract class AbstractControl { */ private _parentMarkedDirty(onlySelf?: boolean): boolean { const parentDirty = this._parent && this._parent.dirty; - return !onlySelf && parentDirty && !this._parent._anyControlsDirty(); + return !onlySelf && !!parentDirty && !this._parent!._anyControlsDirty(); } }