From 8eb1b386b2880a00af947bd428dc579120e084c1 Mon Sep 17 00:00:00 2001 From: JoostK Date: Tue, 24 May 2022 22:07:55 +0200 Subject: [PATCH] refactor(common): improve type of `AsyncPipe._strategy` field (#46128) The field's type does not account for the `null` value that is assigned in the field's initializer. This commit includes `null` in the type for better type-safety. Exising reads are updated with a non-null assertion operation in places where initialization is known to have occurred. PR Close #46128 --- packages/common/src/pipes/async_pipe.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/common/src/pipes/async_pipe.ts b/packages/common/src/pipes/async_pipe.ts index b73ec67059f..06c14ba6e40 100644 --- a/packages/common/src/pipes/async_pipe.ts +++ b/packages/common/src/pipes/async_pipe.ts @@ -86,7 +86,7 @@ export class AsyncPipe implements OnDestroy, PipeTransform { private _subscription: Unsubscribable|Promise|null = null; private _obj: Subscribable|Promise|EventEmitter|null = null; - private _strategy: SubscriptionStrategy = null!; + private _strategy: SubscriptionStrategy|null = null; constructor(private _ref: ChangeDetectorRef) {} @@ -126,7 +126,8 @@ export class AsyncPipe implements OnDestroy, PipeTransform { obj, (value: Object) => this._updateLatestValue(obj, value)); } - private _selectStrategy(obj: Subscribable|Promise|EventEmitter): any { + private _selectStrategy(obj: Subscribable|Promise| + EventEmitter): SubscriptionStrategy { if (ɵisPromise(obj)) { return _promiseStrategy; } @@ -139,7 +140,9 @@ export class AsyncPipe implements OnDestroy, PipeTransform { } private _dispose(): void { - this._strategy.dispose(this._subscription!); + // Note: `dispose` is only called if a subscription has been initialized before, indicating + // that `this._strategy` is also available. + this._strategy!.dispose(this._subscription!); this._latestValue = null; this._subscription = null; this._obj = null;