Skip to content
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

refactor(multiple): eliminate usages of any type (batch 1) #30741

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 8 additions & 2 deletions src/material/snack-bar/simple-snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import {MatSnackBarRef} from './snack-bar-ref';
import {MAT_SNACK_BAR_DATA} from './snack-bar-config';
import {MatSnackBarAction, MatSnackBarActions, MatSnackBarLabel} from './snack-bar-content';

/** Input data for the snack bar. */
export interface TextOnlySnackBarData {
message: string;
action: string;
}

/**
* Interface for a simple snack bar component that has a message and a single action.
*/
export interface TextOnlySnackBar {
data: {message: string; action: string};
data: TextOnlySnackBarData;
snackBarRef: MatSnackBarRef<TextOnlySnackBar>;
action: () => void;
hasAction: boolean;
Expand All @@ -36,7 +42,7 @@ export interface TextOnlySnackBar {
})
export class SimpleSnackBar implements TextOnlySnackBar {
snackBarRef = inject<MatSnackBarRef<SimpleSnackBar>>(MatSnackBarRef);
data = inject(MAT_SNACK_BAR_DATA);
data = inject<TextOnlySnackBarData>(MAT_SNACK_BAR_DATA);

constructor(...args: unknown[]);
constructor() {}
Expand Down
4 changes: 2 additions & 2 deletions src/material/snack-bar/snack-bar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {AriaLivePoliteness} from '@angular/cdk/a11y';
import {Direction} from '@angular/cdk/bidi';

/** Injection token that can be used to access the data that was passed in to a snack bar. */
export const MAT_SNACK_BAR_DATA = new InjectionToken<any>('MatSnackBarData');
export const MAT_SNACK_BAR_DATA = new InjectionToken<unknown>('MatSnackBarData');

/** Possible values for horizontalPosition on MatSnackBarConfig. */
export type MatSnackBarHorizontalPosition = 'start' | 'center' | 'end' | 'left' | 'right';
Expand All @@ -22,7 +22,7 @@ export type MatSnackBarVerticalPosition = 'top' | 'bottom';
/**
* Configuration used when opening a snack-bar.
*/
export class MatSnackBarConfig<D = any> {
export class MatSnackBarConfig<D = unknown> {
/** The politeness level for the MatAriaLiveAnnouncer announcement. */
politeness?: AriaLivePoliteness = 'assertive';

Expand Down
15 changes: 10 additions & 5 deletions src/material/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,11 @@ describe('MatSnackBar', () => {
});

it('should be able to inject arbitrary user data', () => {
const data: BurritosNotificationData = {
burritoType: 'Chimichanga',
};
const snackBarRef = snackBar.openFromComponent(BurritosNotification, {
data: {
burritoType: 'Chimichanga',
},
data,
});

expect(snackBarRef.instance.data)
Expand Down Expand Up @@ -1025,17 +1026,21 @@ class ComponentWithChildViewContainer {
`,
})
class ComponentWithTemplateRef {
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
@ViewChild(TemplateRef) templateRef: TemplateRef<unknown>;
localValue: string;
}

interface BurritosNotificationData {
burritoType: string;
}

/** Simple component for testing ComponentPortal. */
@Component({
template: '<p>Burritos are on the way.</p>',
})
class BurritosNotification {
snackBarRef = inject<MatSnackBarRef<BurritosNotification>>(MatSnackBarRef);
data = inject(MAT_SNACK_BAR_DATA);
data = inject<BurritosNotificationData>(MAT_SNACK_BAR_DATA);
}

@Component({
Expand Down
27 changes: 16 additions & 11 deletions src/material/snack-bar/snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class MatSnackBar implements OnDestroy {
* If there is a parent snack-bar service, all operations should delegate to that parent
* via `_openedSnackBarRef`.
*/
private _snackBarRefAtThisLevel: MatSnackBarRef<any> | null = null;
private _snackBarRefAtThisLevel: MatSnackBarRef<unknown> | null = null;

/** The component that should be rendered as the snack bar's simple component. */
simpleSnackBarComponent = SimpleSnackBar;
Expand All @@ -75,12 +75,12 @@ export class MatSnackBar implements OnDestroy {
handsetCssClass = 'mat-mdc-snack-bar-handset';

/** Reference to the currently opened snackbar at *any* level. */
get _openedSnackBarRef(): MatSnackBarRef<any> | null {
get _openedSnackBarRef(): MatSnackBarRef<unknown> | null {
const parent = this._parentSnackBar;
return parent ? parent._openedSnackBarRef : this._snackBarRefAtThisLevel;
}

set _openedSnackBarRef(value: MatSnackBarRef<any> | null) {
set _openedSnackBarRef(value: MatSnackBarRef<unknown> | null) {
if (this._parentSnackBar) {
this._parentSnackBar._openedSnackBarRef = value;
} else {
Expand All @@ -98,11 +98,11 @@ export class MatSnackBar implements OnDestroy {
* @param component Component to be instantiated.
* @param config Extra configuration for the snack bar.
*/
openFromComponent<T, D = any>(
openFromComponent<T, D = unknown>(
component: ComponentType<T>,
config?: MatSnackBarConfig<D>,
): MatSnackBarRef<T> {
return this._attach(component, config) as MatSnackBarRef<T>;
return this._attach(component, config);
}

/**
Expand All @@ -113,9 +113,9 @@ export class MatSnackBar implements OnDestroy {
* @param config Extra configuration for the snack bar.
*/
openFromTemplate(
template: TemplateRef<any>,
template: TemplateRef<unknown>,
config?: MatSnackBarConfig,
): MatSnackBarRef<EmbeddedViewRef<any>> {
): MatSnackBarRef<EmbeddedViewRef<unknown>> {
return this._attach(template, config);
}

Expand Down Expand Up @@ -187,14 +187,19 @@ export class MatSnackBar implements OnDestroy {
/**
* Places a new component or a template as the content of the snack bar container.
*/
private _attach<T>(content: ComponentType<T>, userConfig?: MatSnackBarConfig): MatSnackBarRef<T>;
private _attach<T>(
content: TemplateRef<T>,
userConfig?: MatSnackBarConfig,
): MatSnackBarRef<EmbeddedViewRef<T>>;
private _attach<T>(
content: ComponentType<T> | TemplateRef<T>,
userConfig?: MatSnackBarConfig,
): MatSnackBarRef<T | EmbeddedViewRef<any>> {
): MatSnackBarRef<T | EmbeddedViewRef<unknown>> {
const config = {...new MatSnackBarConfig(), ...this._defaultConfig, ...userConfig};
const overlayRef = this._createOverlay(config);
const container = this._attachSnackBarContainer(overlayRef, config);
const snackBarRef = new MatSnackBarRef<T | EmbeddedViewRef<any>>(container, overlayRef);
const snackBarRef = new MatSnackBarRef<T | EmbeddedViewRef<unknown>>(container, overlayRef);

if (content instanceof TemplateRef) {
const portal = new TemplatePortal(content, null!, {
Expand Down Expand Up @@ -231,11 +236,11 @@ export class MatSnackBar implements OnDestroy {

this._animateSnackBar(snackBarRef, config);
this._openedSnackBarRef = snackBarRef;
return this._openedSnackBarRef;
return snackBarRef;
}

/** Animates the old snack bar out and the new one in. */
private _animateSnackBar(snackBarRef: MatSnackBarRef<any>, config: MatSnackBarConfig) {
private _animateSnackBar(snackBarRef: MatSnackBarRef<unknown>, config: MatSnackBarConfig) {
// When the snackbar is dismissed, clear the reference to it.
snackBarRef.afterDismissed().subscribe(() => {
// Clear the snackbar ref if it hasn't already been replaced by a newer snackbar.
Expand Down
4 changes: 2 additions & 2 deletions src/material/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ class SimpleMatSortApp {
}
}

class FakeDataSource extends DataSource<any> {
connect(collectionViewer: CollectionViewer): Observable<any[]> {
class FakeDataSource extends DataSource<never> {
connect(collectionViewer: CollectionViewer): Observable<never[]> {
return collectionViewer.viewChange.pipe(map(() => []));
}
disconnect() {}
Expand Down
2 changes: 1 addition & 1 deletion src/material/stepper/step-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {Directive, TemplateRef, inject} from '@angular/core';
selector: 'ng-template[matStepContent]',
})
export class MatStepContent {
_template = inject<TemplateRef<any>>(TemplateRef);
_template = inject<TemplateRef<unknown>>(TemplateRef);

constructor(...args: unknown[]);
constructor() {}
Expand Down
8 changes: 4 additions & 4 deletions src/material/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ describe('MatStepper', () => {

/** Asserts that keyboard interaction works correctly. */
function assertCorrectKeyboardInteraction(
fixture: ComponentFixture<any>,
fixture: ComponentFixture<unknown>,
stepHeaders: DebugElement[],
orientation: StepperOrientation,
) {
Expand Down Expand Up @@ -1742,7 +1742,7 @@ function assertCorrectKeyboardInteraction(

/** Asserts that arrow key direction works correctly in RTL mode. */
function assertArrowKeyInteractionInRtl(
fixture: ComponentFixture<any>,
fixture: ComponentFixture<unknown>,
stepHeaders: DebugElement[],
) {
const stepperComponent = fixture.debugElement.query(By.directive(MatStepper))!.componentInstance;
Expand All @@ -1764,7 +1764,7 @@ function assertArrowKeyInteractionInRtl(

/** Asserts that keyboard interaction works correctly when the user is pressing a modifier key. */
function assertSelectKeyWithModifierInteraction(
fixture: ComponentFixture<any>,
fixture: ComponentFixture<unknown>,
stepHeaders: DebugElement[],
orientation: StepperOrientation,
selectionKey: number,
Expand Down Expand Up @@ -1823,7 +1823,7 @@ function asyncValidator(minLength: number, validationTrigger: Subject<void>): As
function createComponent<T>(
component: Type<T>,
providers: Provider[] = [],
imports: any[] = [],
imports: unknown[] = [],
encapsulation?: ViewEncapsulation,
declarations = [component],
): ComponentFixture<T> {
Expand Down
6 changes: 5 additions & 1 deletion src/material/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ export class MatTableDataSource<T, P extends MatPaginator = MatPaginator> extend
* @returns Whether the filter matches against the data
*/
filterPredicate: (data: T, filter: string) => boolean = (data: T, filter: string): boolean => {
if ((typeof ngDevMode === 'undefined' || ngDevMode) && typeof data !== 'object') {
throw new Error('Default implementation of filterPredicate requires data to be object.');
}

// Transform the filter by converting it to lowercase and removing whitespace.
const transformedFilter = filter.trim().toLowerCase();
// Loops over the values in the array and returns true if any of them match the filter string
return Object.values(data as {[key: string]: any}).some(value =>
return Object.values(data as object).some(value =>
`${value}`.toLowerCase().includes(transformedFilter),
);
};
Expand Down
34 changes: 17 additions & 17 deletions src/material/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ describe('MatTable', () => {
const data = fixture.componentInstance.dataSource!.data;
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
[data[0].a, data[0].b, data[0].c],
[data[1].a, data[1].b, data[1].c],
[data[2].a, data[2].b, data[2].c],
[data[0].a, data[0].b, data[0].c] as string[],
[data[1].a, data[1].b, data[1].c] as string[],
[data[2].a, data[2].b, data[2].c] as string[],
['fourth_row'],
['Footer A', 'Footer B', 'Footer C'],
]);
Expand Down Expand Up @@ -93,10 +93,10 @@ describe('MatTable', () => {
const data = fixture.componentInstance.dataSource!.data;
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
[data[0].a, data[0].b, data[0].c],
[data[1].a, data[1].b, data[1].c],
[data[2].a, data[2].b, data[2].c],
[data[3].a, data[3].b, data[3].c],
[data[0].a, data[0].b, data[0].c] as string[],
[data[1].a, data[1].b, data[1].c] as string[],
[data[2].a, data[2].b, data[2].c] as string[],
[data[3].a, data[3].b, data[3].c] as string[],
]);
});

Expand Down Expand Up @@ -188,9 +188,9 @@ describe('MatTable', () => {
const data = fixture.componentInstance.dataSource!.data;
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
[data[0].a, data[0].b, data[0].c],
[data[1].a, data[1].b, data[1].c],
[data[2].a, data[2].b, data[2].c],
[data[0].a, data[0].b, data[0].c] as string[],
[data[1].a, data[1].b, data[1].c] as string[],
[data[2].a, data[2].b, data[2].c] as string[],
]);
});

Expand All @@ -202,9 +202,9 @@ describe('MatTable', () => {
const data = fixture.componentInstance.dataSource!.data;
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
[data[0].a, data[0].b, data[0].c],
[data[1].a, data[1].b, data[1].c],
[data[2].a, data[2].b, data[2].c],
[data[0].a, data[0].b, data[0].c] as string[],
[data[1].a, data[1].b, data[1].c] as string[],
[data[2].a, data[2].b, data[2].c] as string[],
]);
});

Expand Down Expand Up @@ -386,7 +386,7 @@ describe('MatTable', () => {
]);

// Change the filter to a falsy value that might come in from the view.
dataSource.filter = 0 as any;
dataSource.filter = 0 as unknown as string;
flushMicrotasks();
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
Expand Down Expand Up @@ -633,7 +633,7 @@ describe('MatTable', () => {
['Footer A', 'Footer B', 'Footer C'],
]);

dataSource.data = {} as any;
dataSource.data = {} as TestData[];
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
Expand Down Expand Up @@ -1137,7 +1137,7 @@ function getActualTableContent(tableElement: Element): string[][] {
return actualTableContent.map(row => row.map(cell => cell.textContent!.trim()));
}

export function expectTableToMatchContent(tableElement: Element, expected: any[]) {
export function expectTableToMatchContent(tableElement: Element, expected: string[][]) {
const missedExpectations: string[] = [];
function checkCellContent(actualCell: string, expectedCell: string) {
if (actualCell !== expectedCell) {
Expand All @@ -1163,7 +1163,7 @@ export function expectTableToMatchContent(tableElement: Element, expected: any[]
}

row.forEach((actualCell, cellIndex) => {
const expectedCell = expectedRow ? expectedRow[cellIndex] : null;
const expectedCell = expectedRow[cellIndex];
checkCellContent(actualCell, expectedCell);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/material/tabs/tab-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class SimpleTabBodyApp implements AfterViewInit {
position: number;

@ViewChild(MatTabBody) tabBody: MatTabBody;
@ViewChild(TemplateRef) template: TemplateRef<any>;
@ViewChild(TemplateRef) template: TemplateRef<unknown>;

private readonly _viewContainerRef = inject(ViewContainerRef);

Expand Down
2 changes: 1 addition & 1 deletion src/material/tabs/tab-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const MAT_TAB_CONTENT = new InjectionToken<MatTabContent>('MatTabContent'
providers: [{provide: MAT_TAB_CONTENT, useExisting: MatTabContent}],
})
export class MatTabContent {
template = inject<TemplateRef<any>>(TemplateRef);
template = inject<TemplateRef<unknown>>(TemplateRef);

constructor(...args: unknown[]);
constructor() {}
Expand Down
Loading
Loading