Skip to content

Commit 2045d0b

Browse files
rslawikmmalerba
authored andcommitted
fix(material/datepicker): Return union of view component types (#23000)
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 (cherry picked from commit fafb24d)
1 parent 16a5aba commit 2045d0b

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/material/datepicker/calendar.ts

Lines changed: 5 additions & 13 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,10 @@ 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() {
420+
private _getCurrentViewComponent(): MatMonthView<D> | MatYearView<D> | MatMultiYearView<D> {
421+
// The return type is explicitly written as a union to ensure that the Closure compiler does
422+
// not optimize calls to _init(). Without the explict return type, TypeScript narrows it to
423+
// only the first component type. See https://github.com/angular/components/issues/22996.
432424
return this.monthView || this.yearView || this.multiYearView;
433425
}
434426
}

0 commit comments

Comments
 (0)