fix(common): expand type for "ngForOf" input to work with strict null checks (#31371)

Currently the `ngForOf` input accepts `null` or `undefined` as valid
values. Although when using strict template input type checking
(which will be supported by `ngtsc`), passing `null` or `undefined`
with strict null checks enabled causes a type check failure because
the type for the `ngForOf` input becomes too strict if strict null checks
are enabled. The type of the input needs to be expanded to also accept
`null` or `undefined` to behave consistently regardless of the
`strictNullChecks` flag.

This is necessary because whenever strict input type checking is enabled
by default, most of the Angular projects that use `*ngFor` with the async pipe
will either need to disable template type checking or strict null checks
because the `async` pipe returns `null` if the observable hasn't been
emitted yet.

See for example how this affects the `angular/components` repository and
how much bloat the workaround involves: https://github.com/angular/components/pull/16373/files#r296942696.

PR Close #31371
This commit is contained in:
Paul Gschwendtner
2019-10-03 18:31:28 +02:00
committed by Alex Rickabaugh
parent fee28e20bb
commit c1bb88603e
5 changed files with 12 additions and 10 deletions

View File

@ -239,7 +239,7 @@ export declare class NgComponentOutlet implements OnChanges, OnDestroy {
}
export declare class NgForOf<T> implements DoCheck {
ngForOf: NgIterable<T>;
ngForOf: NgIterable<T> | undefined | null;
ngForTemplate: TemplateRef<NgForOfContext<T>>;
ngForTrackBy: TrackByFunction<T>;
constructor(_viewContainer: ViewContainerRef, _template: TemplateRef<NgForOfContext<T>>, _differs: IterableDiffers);

View File

@ -263,7 +263,7 @@ export declare class DefaultIterableDiffer<V> implements IterableDiffer<V>, Iter
readonly length: number;
constructor(trackByFn?: TrackByFunction<V>);
check(collection: NgIterable<V>): boolean;
diff(collection: NgIterable<V>): DefaultIterableDiffer<V> | null;
diff(collection: NgIterable<V> | null | undefined): DefaultIterableDiffer<V> | null;
forEachAddedItem(fn: (record: IterableChangeRecord_<V>) => void): void;
forEachIdentityChange(fn: (record: IterableChangeRecord_<V>) => void): void;
forEachItem(fn: (record: IterableChangeRecord_<V>) => void): void;
@ -505,7 +505,7 @@ export interface IterableChanges<V> {
}
export interface IterableDiffer<V> {
diff(object: NgIterable<V>): IterableChanges<V> | null;
diff(object: NgIterable<V> | undefined | null): IterableChanges<V> | null;
}
export interface IterableDifferFactory {