Skip to content

refactor(multiple): eliminate usages of any type (batch 2) #30613

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
8 changes: 4 additions & 4 deletions src/google-maps/map-event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import {switchMap} from 'rxjs/operators';

type MapEventManagerTarget =
| {
addListener: (
addListener<T extends unknown[]>(
name: string,
callback: (...args: any[]) => void,
) => google.maps.MapsEventListener | undefined;
callback: (...args: T) => void,
): google.maps.MapsEventListener | undefined;
}
| undefined;

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

Expand Down
8 changes: 4 additions & 4 deletions src/material-date-fns-adapter/adapter/date-fns-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
return new Date();
}

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

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

isDateInstance(obj: any): boolean {
isDateInstance(obj: unknown): obj is Date {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this as boolean even though date-fns says it's returning value is Date. Their implementation is clearly returning a boolean, not a Date-like object as it says here: https://github.com/date-fns/date-fns/blob/313b902b9a72c64501074db9bc2b9897d2db5140/src/isDate/index.ts#L33

In their original javascript implementation, they document that it's returning a boolean too`. I think this might have been a mistake when they changed to TypeScript? https://github.com/date-fns/date-fns/blob/6ba1acc644a444092c672d0d0af33fbe8e7b9481/src/isDate/index.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDateInstance(obj: unknown): obj is Date indicates that isDateInstance is a "type guard". This in practice means that the function returns a boolean value, but also it narrows down the type of the passed argument (obj in this case).

If we had code like

function isFoo(x: unknown): x is Foo { ... }

const test: Foo | Bar = ...;

if (isFoo(test)) {
  // test is of type Foo here
} else {
  // test is of type Bar here
}

https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh okay interesting, thanks for explaining!

return isDate(obj);
}

Expand Down Expand Up @@ -277,7 +277,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
return getSeconds(date);
}

override parseTime(value: any, parseFormat: string | string[]): Date | null {
override parseTime(value: unknown, parseFormat: string | string[]): Date | null {
return this.parse(value, parseFormat);
}

Expand Down
82 changes: 41 additions & 41 deletions src/material-experimental/column-resize/column-resize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class ElementDataSource extends DataSource<PeriodicElement> {
}

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

interface NumberMatchers extends jasmine.Matchers<number> {
isApproximately(expected: number): void;
not: NumberMatchers;
}
declare global {
function expect(actual: number): NumberMatchers;
}

const testCases = [
[MatColumnResizeModule, MatResizeTest, 'opt-in table-based mat-table'],
[MatColumnResizeModule, MatResizeOnPushTest, 'inside OnPush component'],
Expand Down Expand Up @@ -409,12 +417,8 @@ describe('Material Popover Edit', () => {
component.getOverlayThumbElement(2).classList.contains('mat-column-resize-overlay-thumb'),
).toBe(true);

(expect(component.getOverlayThumbElement(0).offsetHeight) as any).isApproximately(
headerRowHeight,
);
(expect(component.getOverlayThumbElement(2).offsetHeight) as any).isApproximately(
headerRowHeight,
);
expect(component.getOverlayThumbElement(0).offsetHeight).isApproximately(headerRowHeight);
expect(component.getOverlayThumbElement(2).offsetHeight).isApproximately(headerRowHeight);

component.beginColumnResizeWithMouse(0);

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

(expect(component.getOverlayThumbElement(0).offsetHeight) as any).isApproximately(
tableHeight,
);
(expect(component.getOverlayThumbTopElement(0).offsetHeight) as any).isApproximately(
headerRowHeight,
);
(expect(component.getOverlayThumbElement(2).offsetHeight) as any).isApproximately(
expect(component.getOverlayThumbElement(0).offsetHeight).isApproximately(tableHeight);
expect(component.getOverlayThumbTopElement(0).offsetHeight).isApproximately(
headerRowHeight,
);
expect(component.getOverlayThumbElement(2).offsetHeight).isApproximately(headerRowHeight);

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

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

component.updateResizeWithMouseInProgress(1);
fixture.detectChanges();
flush();

thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
(expect(thumbPositionDelta) as any).isApproximately(columnPositionDelta);
expect(thumbPositionDelta).isApproximately(columnPositionDelta);

(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 1);
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
expect(component.getTableWidth()).isApproximately(initialTableWidth + 1);
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 1);

component.completeResizeWithMouseInProgress(1);
flush();

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

component.endHoverState();
fixture.detectChanges();
Expand All @@ -508,23 +508,23 @@ describe('Material Popover Edit', () => {
flush();

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

component.updateResizeWithMouseInProgress(1);
fixture.detectChanges();
flush();

thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;

(expect(component.getTableWidth()) as any).toBe(initialTableWidth);
(expect(component.getColumnWidth(1)) as any).toBe(initialColumnWidth);
expect(component.getTableWidth()).toBe(initialTableWidth);
expect(component.getColumnWidth(1)).toBe(initialColumnWidth);

component.completeResizeWithMouseInProgress(1);
flush();

(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 1);
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
expect(component.getTableWidth()).isApproximately(initialTableWidth + 1);
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth + 1);

component.endHoverState();
fixture.detectChanges();
Expand Down Expand Up @@ -562,18 +562,18 @@ describe('Material Popover Edit', () => {

let thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
let columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
(expect(thumbPositionDelta) as any).isApproximately(columnPositionDelta);
expect(thumbPositionDelta).isApproximately(columnPositionDelta);

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

dispatchKeyboardEvent(document, 'keyup', ESCAPE);
flush();

(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth);
(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth);
expect(component.getColumnWidth(1)).isApproximately(initialColumnWidth);
expect(component.getTableWidth()).isApproximately(initialTableWidth);

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

let resize: ColumnSize | null = null;
let resize: ColumnSize | null = null as ColumnSize | null;
component.columnResize.columnResizeNotifier.resizeCompleted.subscribe(size => {
resize = size;
});
Expand All @@ -596,7 +596,7 @@ describe('Material Popover Edit', () => {
fixture.detectChanges();
flush();

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

component.endHoverState();
fixture.detectChanges();
Expand Down Expand Up @@ -626,12 +626,12 @@ describe('Material Popover Edit', () => {

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

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

(expect(component.getColumnWidth(1)) as any).isApproximately(173);
expect(component.getColumnWidth(1)).isApproximately(173);
}));
});
}
Expand Down Expand Up @@ -660,13 +660,13 @@ describe('Material Popover Edit', () => {
}));

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

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

flush();

(expect(component.getColumnWidth(1)) as any).isApproximately(300);
expect(component.getColumnWidth(1)).isApproximately(300);
}));

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

it('persists the user-triggered size update (live updates off)', fakeAsync(() => {
Expand All @@ -714,7 +714,7 @@ describe('Material Popover Edit', () => {
const {tableId, columnId, sizePx} = columnSizeStore.setSizeCalls[0];
expect(tableId).toBe('theTable');
expect(columnId).toBe('name');
(expect(sizePx) as any).isApproximately(initialColumnWidth + 5);
expect(sizePx).isApproximately(initialColumnWidth + 5);
}));
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/material-luxon-adapter/adapter/luxon-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
return this._useUTC ? LuxonDateTime.utc(options) : LuxonDateTime.local(options);
}

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

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

isDateInstance(obj: any): boolean {
isDateInstance(obj: unknown): obj is LuxonDateTime {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - isn't this definitely a boolean?

return obj instanceof LuxonDateTime;
}

Expand Down Expand Up @@ -315,7 +315,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
return date.second;
}

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

if ((!result || !this.isValid(result)) && typeof value === 'string') {
Expand Down
8 changes: 4 additions & 4 deletions src/material-moment-adapter/adapter/moment-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
return this._createMoment().locale(this.locale);
}

parse(value: any, parseFormat: string | string[]): Moment | null {
parse(value: unknown, parseFormat: string | string[]): Moment | null {
if (value && typeof value == 'string') {
return this._createMoment(value, parseFormat, this.locale);
}
Expand Down Expand Up @@ -223,7 +223,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
* (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects into valid Moments and empty
* string into null. Returns an invalid date for all other values.
*/
override deserialize(value: any): Moment | null {
override deserialize(value: unknown): Moment | null {
let date;
if (value instanceof Date) {
date = this._createMoment(value).locale(this.locale);
Expand All @@ -243,7 +243,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
return super.deserialize(value);
}

isDateInstance(obj: any): boolean {
isDateInstance(obj: unknown): obj is Moment {
return moment.isMoment(obj);
}

Expand Down Expand Up @@ -285,7 +285,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
return date.seconds();
}

override parseTime(value: any, parseFormat: string | string[]): Moment | null {
override parseTime(value: unknown, parseFormat: string | string[]): Moment | null {
return this.parse(value, parseFormat);
}

Expand Down
13 changes: 11 additions & 2 deletions src/universal-app/hydration.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import {browser, by, element, ExpectedConditions} from 'protractor';

declare global {
interface Window {
ngDevMode: {
hydratedComponents: number;
componentsSkippedHydration: number;
};
}
}

describe('hydration e2e', () => {
beforeEach(async () => {
await browser.waitForAngularEnabled(false);
Expand Down Expand Up @@ -27,7 +36,7 @@ async function getHydrationState() {
hydratedComponents: number;
componentsSkippedHydration: number;
}>(() => ({
hydratedComponents: (window as any).ngDevMode.hydratedComponents,
componentsSkippedHydration: (window as any).ngDevMode.componentsSkippedHydration,
hydratedComponents: window.ngDevMode.hydratedComponents,
componentsSkippedHydration: window.ngDevMode.componentsSkippedHydration,
}));
}
11 changes: 9 additions & 2 deletions src/universal-app/kitchen-sink/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ import {MatTooltipModule} from '@angular/material/tooltip';
import {YouTubePlayer} from '@angular/youtube-player';
import {Observable, of as observableOf} from 'rxjs';

export class TableDataSource extends DataSource<any> {
connect(): Observable<any> {
interface ElementItem {
position: number;
name: string;
weight: number;
symbol: string;
}

export class TableDataSource extends DataSource<ElementItem> {
connect(): Observable<ElementItem[]> {
return observableOf([
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
Expand Down
Loading
Loading