diff --git a/packages/common/src/directives/ng_for_of.ts b/packages/common/src/directives/ng_for_of.ts index 04276d92d1..446a967e92 100644 --- a/packages/common/src/directives/ng_for_of.ts +++ b/packages/common/src/directives/ng_for_of.ts @@ -129,7 +129,7 @@ export class NgForOf implements DoCheck { * [template input variable](guide/structural-directives#template-input-variable). */ @Input() - set ngForOf(ngForOf: NgIterable) { + set ngForOf(ngForOf: NgIterable|undefined|null) { this._ngForOf = ngForOf; this._ngForOfDirty = true; } @@ -165,8 +165,7 @@ export class NgForOf implements DoCheck { get ngForTrackBy(): TrackByFunction { return this._trackByFn; } - // TODO(issue/24571): remove '!'. - private _ngForOf !: NgIterable; + private _ngForOf: NgIterable|undefined|null = null; private _ngForOfDirty: boolean = true; private _differ: IterableDiffer|null = null; // TODO(issue/24571): remove '!'. @@ -219,8 +218,11 @@ export class NgForOf implements DoCheck { (item: IterableChangeRecord, adjustedPreviousIndex: number | null, currentIndex: number | null) => { if (item.previousIndex == null) { + // NgForOf is never "null" or "undefined" here because the differ detected + // that a new item needs to be inserted from the iterable. This implies that + // there is an iterable value for "_ngForOf". const view = this._viewContainer.createEmbeddedView( - this._template, new NgForOfContext(null !, this._ngForOf, -1, -1), + this._template, new NgForOfContext(null !, this._ngForOf !, -1, -1), currentIndex === null ? undefined : currentIndex); const tuple = new RecordViewTuple(item, view); insertTuples.push(tuple); @@ -243,7 +245,7 @@ export class NgForOf implements DoCheck { const viewRef = >>this._viewContainer.get(i); viewRef.context.index = i; viewRef.context.count = ilen; - viewRef.context.ngForOf = this._ngForOf; + viewRef.context.ngForOf = this._ngForOf !; } changes.forEachIdentityChange((record: any) => { diff --git a/packages/core/src/change_detection/differs/default_iterable_differ.ts b/packages/core/src/change_detection/differs/default_iterable_differ.ts index 1d909df30b..dd6d22a06d 100644 --- a/packages/core/src/change_detection/differs/default_iterable_differ.ts +++ b/packages/core/src/change_detection/differs/default_iterable_differ.ts @@ -146,7 +146,7 @@ export class DefaultIterableDiffer implements IterableDiffer, IterableChan } } - diff(collection: NgIterable): DefaultIterableDiffer|null { + diff(collection: NgIterable|null|undefined): DefaultIterableDiffer|null { if (collection == null) collection = []; if (!isListLikeIterable(collection)) { throw new Error( diff --git a/packages/core/src/change_detection/differs/iterable_differs.ts b/packages/core/src/change_detection/differs/iterable_differs.ts index c80508eb6d..e81ba67413 100644 --- a/packages/core/src/change_detection/differs/iterable_differs.ts +++ b/packages/core/src/change_detection/differs/iterable_differs.ts @@ -34,7 +34,7 @@ export interface IterableDiffer { * @returns an object describing the difference. The return value is only valid until the next * `diff()` invocation. */ - diff(object: NgIterable): IterableChanges|null; + diff(object: NgIterable|undefined|null): IterableChanges|null; } /** diff --git a/tools/public_api_guard/common/common.d.ts b/tools/public_api_guard/common/common.d.ts index 24cc338f19..a9e029fd91 100644 --- a/tools/public_api_guard/common/common.d.ts +++ b/tools/public_api_guard/common/common.d.ts @@ -239,7 +239,7 @@ export declare class NgComponentOutlet implements OnChanges, OnDestroy { } export declare class NgForOf implements DoCheck { - ngForOf: NgIterable; + ngForOf: NgIterable | undefined | null; ngForTemplate: TemplateRef>; ngForTrackBy: TrackByFunction; constructor(_viewContainer: ViewContainerRef, _template: TemplateRef>, _differs: IterableDiffers); diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index 9362b58a84..bf81f43698 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -263,7 +263,7 @@ export declare class DefaultIterableDiffer implements IterableDiffer, Iter readonly length: number; constructor(trackByFn?: TrackByFunction); check(collection: NgIterable): boolean; - diff(collection: NgIterable): DefaultIterableDiffer | null; + diff(collection: NgIterable | null | undefined): DefaultIterableDiffer | null; forEachAddedItem(fn: (record: IterableChangeRecord_) => void): void; forEachIdentityChange(fn: (record: IterableChangeRecord_) => void): void; forEachItem(fn: (record: IterableChangeRecord_) => void): void; @@ -505,7 +505,7 @@ export interface IterableChanges { } export interface IterableDiffer { - diff(object: NgIterable): IterableChanges | null; + diff(object: NgIterable | undefined | null): IterableChanges | null; } export interface IterableDifferFactory {