Skip to content

Commit 26370e0

Browse files
committed
fix(material/datepicker): Return union of view component types
TypeScript inferred the return type of `_getCurrentViewComponent` as the type of the month view only. Tsickle propagates that to the JavaScript compiler which optimizes the code as `monthView._init` was the only method that could be called. This causes runtime errors. Fixed #22996
1 parent b57b686 commit 26370e0

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/material/datepicker/calendar.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,18 +385,7 @@ export class MatCalendar<D> implements AfterContentInit, AfterViewChecked, OnDes
385385

386386
/** Updates today's date after an update of the active date */
387387
updateTodaysDate() {
388-
const currentView = this.currentView;
389-
let view: MatMonthView<D> | MatYearView<D> | MatMultiYearView<D>;
390-
391-
if (currentView === 'month') {
392-
view = this.monthView;
393-
} else if (currentView === 'year') {
394-
view = this.yearView;
395-
} else {
396-
view = this.multiYearView;
397-
}
398-
399-
view._init();
388+
this._getCurrentViewComponent()._init();
400389
}
401390

402391
/** Handles date selection in the month view. */
@@ -428,7 +417,16 @@ export class MatCalendar<D> implements AfterContentInit, AfterViewChecked, OnDes
428417
}
429418

430419
/** Returns the component instance that corresponds to the current calendar view. */
431-
private _getCurrentViewComponent() {
432-
return this.monthView || this.yearView || this.multiYearView;
420+
private _getCurrentViewComponent(): MatMonthView<D> | MatYearView<D> | MatMultiYearView<D> {
421+
// The return type must be a union to ensure that the Closure compiler does not optimize the
422+
// calls to _init(). See https://github.com/angular/components/issues/22996.
423+
switch (this.currentView) {
424+
case 'month':
425+
return this.monthView;
426+
case 'year':
427+
return this.yearView;
428+
case 'multi-year':
429+
return this.multiYearView;
430+
}
433431
}
434432
}

0 commit comments

Comments
 (0)