Skip to content

Commit 25cd400

Browse files
authored
refactor(multiple): eliminate usages of any type (batch 2) (angular#30613)
* fix(youtube-player): Eliminate `any` casts * fix(multiple): ngDevMode always defined in hydration e2e test * fix(google-maps): Replace any types with unknown * fix(multiple): Add ElementItem interface * fix(material-moment-adapter): Replace any with unknowns * fix(material-luxon-adapter): Replace any with unknown * fix(material-date-fns-adapter): Replace any with unknown * fix(material-experimental/column-resize): Add NumberMatchers interface in tests * refactor(multiple): improve types * refactor(google-maps): improve `addListener` type
1 parent 3465f92 commit 25cd400

File tree

9 files changed

+106
-84
lines changed

9 files changed

+106
-84
lines changed

src/google-maps/map-event-manager.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import {switchMap} from 'rxjs/operators';
1212

1313
type MapEventManagerTarget =
1414
| {
15-
addListener: (
15+
addListener<T extends unknown[]>(
1616
name: string,
17-
callback: (...args: any[]) => void,
18-
) => google.maps.MapsEventListener | undefined;
17+
callback: (...args: T) => void,
18+
): google.maps.MapsEventListener | undefined;
1919
}
2020
| undefined;
2121

2222
/** Manages event on a Google Maps object, ensuring that events are added only when necessary. */
2323
export class MapEventManager {
2424
/** Pending listeners that were added before the target was set. */
25-
private _pending: {observable: Observable<any>; observer: Subscriber<any>}[] = [];
25+
private _pending: {observable: Observable<unknown>; observer: Subscriber<unknown>}[] = [];
2626
private _listeners: google.maps.MapsEventListener[] = [];
2727
private _targetStream = new BehaviorSubject<MapEventManagerTarget>(undefined);
2828

src/material-date-fns-adapter/adapter/date-fns-adapter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
161161
return new Date();
162162
}
163163

164-
parse(value: any, parseFormat: string | string[]): Date | null {
164+
parse(value: unknown, parseFormat: string | string[]): Date | null {
165165
if (typeof value == 'string' && value.length > 0) {
166166
const iso8601Date = parseISO(value);
167167

@@ -222,7 +222,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
222222
* (https://www.ietf.org/rfc/rfc3339.txt) into valid Dates and empty string into null. Returns an
223223
* invalid date for all other values.
224224
*/
225-
override deserialize(value: any): Date | null {
225+
override deserialize(value: unknown): Date | null {
226226
if (typeof value === 'string') {
227227
if (!value) {
228228
return null;
@@ -235,7 +235,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
235235
return super.deserialize(value);
236236
}
237237

238-
isDateInstance(obj: any): boolean {
238+
isDateInstance(obj: unknown): obj is Date {
239239
return isDate(obj);
240240
}
241241

@@ -277,7 +277,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
277277
return getSeconds(date);
278278
}
279279

280-
override parseTime(value: any, parseFormat: string | string[]): Date | null {
280+
override parseTime(value: unknown, parseFormat: string | string[]): Date | null {
281281
return this.parse(value, parseFormat);
282282
}
283283

src/material-experimental/column-resize/column-resize.spec.ts

+41-41
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class ElementDataSource extends DataSource<PeriodicElement> {
333333
}
334334

335335
// There's 1px of variance between different browsers in terms of positioning.
336-
const approximateMatcher = {
336+
const approximateMatcher: jasmine.CustomMatcherFactories = {
337337
isApproximately: () => ({
338338
compare: (actual: number, expected: number) => {
339339
const result = {
@@ -348,6 +348,14 @@ const approximateMatcher = {
348348
}),
349349
};
350350

351+
interface NumberMatchers extends jasmine.Matchers<number> {
352+
isApproximately(expected: number): void;
353+
not: NumberMatchers;
354+
}
355+
declare global {
356+
function expect(actual: number): NumberMatchers;
357+
}
358+
351359
const testCases = [
352360
[MatColumnResizeModule, MatResizeTest, 'opt-in table-based mat-table'],
353361
[MatColumnResizeModule, MatResizeOnPushTest, 'inside OnPush component'],
@@ -409,12 +417,8 @@ describe('Material Popover Edit', () => {
409417
component.getOverlayThumbElement(2).classList.contains('mat-column-resize-overlay-thumb'),
410418
).toBe(true);
411419

412-
(expect(component.getOverlayThumbElement(0).offsetHeight) as any).isApproximately(
413-
headerRowHeight,
414-
);
415-
(expect(component.getOverlayThumbElement(2).offsetHeight) as any).isApproximately(
416-
headerRowHeight,
417-
);
420+
expect(component.getOverlayThumbElement(0).offsetHeight).isApproximately(headerRowHeight);
421+
expect(component.getOverlayThumbElement(2).offsetHeight).isApproximately(headerRowHeight);
418422

419423
component.beginColumnResizeWithMouse(0);
420424

@@ -425,15 +429,11 @@ describe('Material Popover Edit', () => {
425429
component.getOverlayThumbElement(2).classList.contains('mat-column-resize-overlay-thumb'),
426430
).toBe(true);
427431

428-
(expect(component.getOverlayThumbElement(0).offsetHeight) as any).isApproximately(
429-
tableHeight,
430-
);
431-
(expect(component.getOverlayThumbTopElement(0).offsetHeight) as any).isApproximately(
432-
headerRowHeight,
433-
);
434-
(expect(component.getOverlayThumbElement(2).offsetHeight) as any).isApproximately(
432+
expect(component.getOverlayThumbElement(0).offsetHeight).isApproximately(tableHeight);
433+
expect(component.getOverlayThumbTopElement(0).offsetHeight).isApproximately(
435434
headerRowHeight,
436435
);
436+
expect(component.getOverlayThumbElement(2).offsetHeight).isApproximately(headerRowHeight);
437437

438438
component.completeResizeWithMouseInProgress(0);
439439
component.endHoverState();
@@ -462,31 +462,31 @@ describe('Material Popover Edit', () => {
462462
let columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
463463
// let nextColumnPositionDelta =
464464
// component.getColumnOriginPosition(2) - initialNextColumnPosition;
465-
(expect(thumbPositionDelta) as any).isApproximately(columnPositionDelta);
465+
expect(thumbPositionDelta).isApproximately(columnPositionDelta);
466466
// TODO: This was commented out after switching from the legacy table to the current
467467
// MDC-based table. This failed by being inaccurate by several pixels.
468-
// (expect(nextColumnPositionDelta) as any).isApproximately(columnPositionDelta);
468+
// expect(nextColumnPositionDelta).isApproximately(columnPositionDelta);
469469

470470
// TODO: This was commented out after switching from the legacy table to the current
471471
// MDC-based table. This failed by being inaccurate by several pixels.
472-
// (expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 5);
473-
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 5);
472+
// expect(component.getTableWidth()).isApproximately(initialTableWidth + 5);
473+
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 5);
474474

475475
component.updateResizeWithMouseInProgress(1);
476476
fixture.detectChanges();
477477
flush();
478478

479479
thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
480480
columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
481-
(expect(thumbPositionDelta) as any).isApproximately(columnPositionDelta);
481+
expect(thumbPositionDelta).isApproximately(columnPositionDelta);
482482

483-
(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 1);
484-
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
483+
expect(component.getTableWidth()).isApproximately(initialTableWidth + 1);
484+
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 1);
485485

486486
component.completeResizeWithMouseInProgress(1);
487487
flush();
488488

489-
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
489+
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 1);
490490

491491
component.endHoverState();
492492
fixture.detectChanges();
@@ -508,23 +508,23 @@ describe('Material Popover Edit', () => {
508508
flush();
509509

510510
let thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
511-
(expect(thumbPositionDelta) as any).isApproximately(5);
512-
(expect(component.getColumnWidth(1)) as any).toBe(initialColumnWidth);
511+
expect(thumbPositionDelta).isApproximately(5);
512+
expect(component.getColumnWidth(1)).toBe(initialColumnWidth);
513513

514514
component.updateResizeWithMouseInProgress(1);
515515
fixture.detectChanges();
516516
flush();
517517

518518
thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
519519

520-
(expect(component.getTableWidth()) as any).toBe(initialTableWidth);
521-
(expect(component.getColumnWidth(1)) as any).toBe(initialColumnWidth);
520+
expect(component.getTableWidth()).toBe(initialTableWidth);
521+
expect(component.getColumnWidth(1)).toBe(initialColumnWidth);
522522

523523
component.completeResizeWithMouseInProgress(1);
524524
flush();
525525

526-
(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 1);
527-
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
526+
expect(component.getTableWidth()).isApproximately(initialTableWidth + 1);
527+
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 1);
528528

529529
component.endHoverState();
530530
fixture.detectChanges();
@@ -562,18 +562,18 @@ describe('Material Popover Edit', () => {
562562

563563
let thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
564564
let columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
565-
(expect(thumbPositionDelta) as any).isApproximately(columnPositionDelta);
565+
expect(thumbPositionDelta).isApproximately(columnPositionDelta);
566566

567-
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 5);
567+
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 5);
568568
// TODO: This was commented out after switching from the legacy table to the current
569569
// MDC-based table. This failed by being inaccurate by several pixels.
570-
// (expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 5);
570+
// expect(component.getTableWidth()).isApproximately(initialTableWidth + 5);
571571

572572
dispatchKeyboardEvent(document, 'keyup', ESCAPE);
573573
flush();
574574

575-
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth);
576-
(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth);
575+
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth);
576+
expect(component.getTableWidth()).isApproximately(initialTableWidth);
577577

578578
component.endHoverState();
579579
fixture.detectChanges();
@@ -582,7 +582,7 @@ describe('Material Popover Edit', () => {
582582
it('notifies subscribers of a completed resize via ColumnResizeNotifier', fakeAsync(() => {
583583
const initialColumnWidth = component.getColumnWidth(1);
584584

585-
let resize: ColumnSize | null = null;
585+
let resize: ColumnSize | null = null as ColumnSize | null;
586586
component.columnResize.columnResizeNotifier.resizeCompleted.subscribe(size => {
587587
resize = size;
588588
});
@@ -596,7 +596,7 @@ describe('Material Popover Edit', () => {
596596
fixture.detectChanges();
597597
flush();
598598

599-
expect(resize).toEqual({columnId: 'name', size: initialColumnWidth + 5} as any);
599+
expect(resize).toEqual({columnId: 'name', size: initialColumnWidth + 5});
600600

601601
component.endHoverState();
602602
fixture.detectChanges();
@@ -626,12 +626,12 @@ describe('Material Popover Edit', () => {
626626

627627
it('performs a column resize triggered via ColumnResizeNotifier', fakeAsync(() => {
628628
// Pre-verify that we are not updating the size to the initial size.
629-
(expect(component.getColumnWidth(1)) as any).not.isApproximately(173);
629+
expect(component.getColumnWidth(1)).not.isApproximately(173);
630630

631631
component.columnResize.columnResizeNotifier.resize('name', 173);
632632
flush();
633633

634-
(expect(component.getColumnWidth(1)) as any).isApproximately(173);
634+
expect(component.getColumnWidth(1)).isApproximately(173);
635635
}));
636636
});
637637
}
@@ -660,13 +660,13 @@ describe('Material Popover Edit', () => {
660660
}));
661661

662662
it('applies the persisted size', fakeAsync(() => {
663-
(expect(component.getColumnWidth(1)).not as any).isApproximately(300);
663+
expect(component.getColumnWidth(1)).not.isApproximately(300);
664664

665665
columnSizeStore.emitSize('theTable', 'name', 300);
666666

667667
flush();
668668

669-
(expect(component.getColumnWidth(1)) as any).isApproximately(300);
669+
expect(component.getColumnWidth(1)).isApproximately(300);
670670
}));
671671

672672
it('persists the user-triggered size update', fakeAsync(() => {
@@ -689,7 +689,7 @@ describe('Material Popover Edit', () => {
689689
const {tableId, columnId, sizePx} = columnSizeStore.setSizeCalls[0];
690690
expect(tableId).toBe('theTable');
691691
expect(columnId).toBe('name');
692-
(expect(sizePx) as any).isApproximately(initialColumnWidth + 5);
692+
expect(sizePx).isApproximately(initialColumnWidth + 5);
693693
}));
694694

695695
it('persists the user-triggered size update (live updates off)', fakeAsync(() => {
@@ -714,7 +714,7 @@ describe('Material Popover Edit', () => {
714714
const {tableId, columnId, sizePx} = columnSizeStore.setSizeCalls[0];
715715
expect(tableId).toBe('theTable');
716716
expect(columnId).toBe('name');
717-
(expect(sizePx) as any).isApproximately(initialColumnWidth + 5);
717+
expect(sizePx).isApproximately(initialColumnWidth + 5);
718718
}));
719719
});
720720
});

src/material-luxon-adapter/adapter/luxon-date-adapter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
177177
return this._useUTC ? LuxonDateTime.utc(options) : LuxonDateTime.local(options);
178178
}
179179

180-
parse(value: any, parseFormat: string | string[]): LuxonDateTime | null {
180+
parse(value: unknown, parseFormat: string | string[]): LuxonDateTime | null {
181181
const options: LuxonDateTimeOptions = this._getOptions();
182182

183183
if (typeof value == 'string' && value.length > 0) {
@@ -245,7 +245,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
245245
* (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects into valid DateTime and empty
246246
* string into null. Returns an invalid date for all other values.
247247
*/
248-
override deserialize(value: any): LuxonDateTime | null {
248+
override deserialize(value: unknown): LuxonDateTime | null {
249249
const options = this._getOptions();
250250
let date: LuxonDateTime | undefined;
251251
if (value instanceof Date) {
@@ -263,7 +263,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
263263
return super.deserialize(value);
264264
}
265265

266-
isDateInstance(obj: any): boolean {
266+
isDateInstance(obj: unknown): obj is LuxonDateTime {
267267
return obj instanceof LuxonDateTime;
268268
}
269269

@@ -315,7 +315,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
315315
return date.second;
316316
}
317317

318-
override parseTime(value: any, parseFormat: string | string[]): LuxonDateTime | null {
318+
override parseTime(value: unknown, parseFormat: string | string[]): LuxonDateTime | null {
319319
const result = this.parse(value, parseFormat);
320320

321321
if ((!result || !this.isValid(result)) && typeof value === 'string') {

src/material-moment-adapter/adapter/moment-date-adapter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
187187
return this._createMoment().locale(this.locale);
188188
}
189189

190-
parse(value: any, parseFormat: string | string[]): Moment | null {
190+
parse(value: unknown, parseFormat: string | string[]): Moment | null {
191191
if (value && typeof value == 'string') {
192192
return this._createMoment(value, parseFormat, this.locale);
193193
}
@@ -223,7 +223,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
223223
* (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects into valid Moments and empty
224224
* string into null. Returns an invalid date for all other values.
225225
*/
226-
override deserialize(value: any): Moment | null {
226+
override deserialize(value: unknown): Moment | null {
227227
let date;
228228
if (value instanceof Date) {
229229
date = this._createMoment(value).locale(this.locale);
@@ -243,7 +243,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
243243
return super.deserialize(value);
244244
}
245245

246-
isDateInstance(obj: any): boolean {
246+
isDateInstance(obj: unknown): obj is Moment {
247247
return moment.isMoment(obj);
248248
}
249249

@@ -285,7 +285,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
285285
return date.seconds();
286286
}
287287

288-
override parseTime(value: any, parseFormat: string | string[]): Moment | null {
288+
override parseTime(value: unknown, parseFormat: string | string[]): Moment | null {
289289
return this.parse(value, parseFormat);
290290
}
291291

src/universal-app/hydration.e2e.spec.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import {browser, by, element, ExpectedConditions} from 'protractor';
22

3+
declare global {
4+
interface Window {
5+
ngDevMode: {
6+
hydratedComponents: number;
7+
componentsSkippedHydration: number;
8+
};
9+
}
10+
}
11+
312
describe('hydration e2e', () => {
413
beforeEach(async () => {
514
await browser.waitForAngularEnabled(false);
@@ -27,7 +36,7 @@ async function getHydrationState() {
2736
hydratedComponents: number;
2837
componentsSkippedHydration: number;
2938
}>(() => ({
30-
hydratedComponents: (window as any).ngDevMode.hydratedComponents,
31-
componentsSkippedHydration: (window as any).ngDevMode.componentsSkippedHydration,
39+
hydratedComponents: window.ngDevMode.hydratedComponents,
40+
componentsSkippedHydration: window.ngDevMode.componentsSkippedHydration,
3241
}));
3342
}

src/universal-app/kitchen-sink/kitchen-sink.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ import {MatTooltipModule} from '@angular/material/tooltip';
6262
import {YouTubePlayer} from '@angular/youtube-player';
6363
import {Observable, of as observableOf} from 'rxjs';
6464

65-
export class TableDataSource extends DataSource<any> {
66-
connect(): Observable<any> {
65+
interface ElementItem {
66+
position: number;
67+
name: string;
68+
weight: number;
69+
symbol: string;
70+
}
71+
72+
export class TableDataSource extends DataSource<ElementItem> {
73+
connect(): Observable<ElementItem[]> {
6774
return observableOf([
6875
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
6976
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},

0 commit comments

Comments
 (0)