Skip to content

fix(cdk/scrolling): expand type for "cdkVirtualForOf" input to work with strict null checks #17421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/cdk/scrolling/virtual-for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy

/** The DataSource to display. */
@Input()
get cdkVirtualForOf(): DataSource<T> | Observable<T[]> | NgIterable<T> {
get cdkVirtualForOf(): DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined {
return this._cdkVirtualForOf;
}
set cdkVirtualForOf(value: DataSource<T> | Observable<T[]> | NgIterable<T>) {
set cdkVirtualForOf(value: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined) {
this._cdkVirtualForOf = value;
const ds = isDataSource(value) ? value :
// Slice the value if its an NgIterable to ensure we're working with an array.
new ArrayDataSource<T>(
value instanceof Observable ? value : Array.prototype.slice.call(value || []));
this._dataSourceChanges.next(ds);
}
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined;

/**
* The `TrackByFunction` to use for tracking changes. The `TrackByFunction` takes the index and
Expand Down Expand Up @@ -363,7 +363,9 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
// comment node which can throw off the move when it's being repeated for all items.
return this._viewContainerRef.createEmbeddedView(this._template, {
$implicit: null!,
cdkVirtualForOf: this._cdkVirtualForOf,
// It's guaranteed that the iterable is not "undefined" or "null" because we only
// generate views for elements if the "cdkVirtualForOf" iterable has elements.
cdkVirtualForOf: this._cdkVirtualForOf!,
index: -1,
count: -1,
first: false,
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/cdk/scrolling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export declare class CdkScrollable implements OnInit, OnDestroy {
}

export declare class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy {
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;
cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined;
cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined;
cdkVirtualForTemplate: TemplateRef<CdkVirtualForOfContext<T>>;
cdkVirtualForTemplateCacheSize: number;
cdkVirtualForTrackBy: TrackByFunction<T> | undefined;
Expand Down