Skip to content

Commit 06ed27d

Browse files
committed
fix(material/table): Remove any
Also added new error throwing logic inside table-data-source when data is not an object.
1 parent 0d8bdf9 commit 06ed27d

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/material/table/table-data-source.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,14 @@ export class MatTableDataSource<T, P extends MatPaginator = MatPaginator> extend
229229
* @returns Whether the filter matches against the data
230230
*/
231231
filterPredicate: (data: T, filter: string) => boolean = (data: T, filter: string): boolean => {
232+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && typeof data !== 'object') {
233+
throw new Error('Default implementation of filterPredicate requires data to be object.');
234+
}
235+
232236
// Transform the filter by converting it to lowercase and removing whitespace.
233237
const transformedFilter = filter.trim().toLowerCase();
234238
// Loops over the values in the array and returns true if any of them match the filter string
235-
return Object.values(data as {[key: string]: any}).some(value =>
239+
return Object.values(data as object).some(value =>
236240
`${value}`.toLowerCase().includes(transformedFilter),
237241
);
238242
};

src/material/table/table.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ describe('MatTable', () => {
4545
const data = fixture.componentInstance.dataSource!.data;
4646
expectTableToMatchContent(tableElement, [
4747
['Column A', 'Column B', 'Column C'],
48-
[data[0].a, data[0].b, data[0].c],
49-
[data[1].a, data[1].b, data[1].c],
50-
[data[2].a, data[2].b, data[2].c],
48+
[data[0].a, data[0].b, data[0].c] as string[],
49+
[data[1].a, data[1].b, data[1].c] as string[],
50+
[data[2].a, data[2].b, data[2].c] as string[],
5151
['fourth_row'],
5252
['Footer A', 'Footer B', 'Footer C'],
5353
]);
@@ -93,10 +93,10 @@ describe('MatTable', () => {
9393
const data = fixture.componentInstance.dataSource!.data;
9494
expectTableToMatchContent(tableElement, [
9595
['Column A', 'Column B', 'Column C'],
96-
[data[0].a, data[0].b, data[0].c],
97-
[data[1].a, data[1].b, data[1].c],
98-
[data[2].a, data[2].b, data[2].c],
99-
[data[3].a, data[3].b, data[3].c],
96+
[data[0].a, data[0].b, data[0].c] as string[],
97+
[data[1].a, data[1].b, data[1].c] as string[],
98+
[data[2].a, data[2].b, data[2].c] as string[],
99+
[data[3].a, data[3].b, data[3].c] as string[],
100100
]);
101101
});
102102

@@ -188,9 +188,9 @@ describe('MatTable', () => {
188188
const data = fixture.componentInstance.dataSource!.data;
189189
expectTableToMatchContent(tableElement, [
190190
['Column A', 'Column B', 'Column C'],
191-
[data[0].a, data[0].b, data[0].c],
192-
[data[1].a, data[1].b, data[1].c],
193-
[data[2].a, data[2].b, data[2].c],
191+
[data[0].a, data[0].b, data[0].c] as string[],
192+
[data[1].a, data[1].b, data[1].c] as string[],
193+
[data[2].a, data[2].b, data[2].c] as string[],
194194
]);
195195
});
196196

@@ -202,9 +202,9 @@ describe('MatTable', () => {
202202
const data = fixture.componentInstance.dataSource!.data;
203203
expectTableToMatchContent(tableElement, [
204204
['Column A', 'Column B', 'Column C'],
205-
[data[0].a, data[0].b, data[0].c],
206-
[data[1].a, data[1].b, data[1].c],
207-
[data[2].a, data[2].b, data[2].c],
205+
[data[0].a, data[0].b, data[0].c] as string[],
206+
[data[1].a, data[1].b, data[1].c] as string[],
207+
[data[2].a, data[2].b, data[2].c] as string[],
208208
]);
209209
});
210210

@@ -386,7 +386,7 @@ describe('MatTable', () => {
386386
]);
387387

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

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

1140-
export function expectTableToMatchContent(tableElement: Element, expected: any[]) {
1140+
export function expectTableToMatchContent(tableElement: Element, expected: string[][]) {
11411141
const missedExpectations: string[] = [];
11421142
function checkCellContent(actualCell: string, expectedCell: string) {
11431143
if (actualCell !== expectedCell) {
@@ -1163,7 +1163,7 @@ export function expectTableToMatchContent(tableElement: Element, expected: any[]
11631163
}
11641164

11651165
row.forEach((actualCell, cellIndex) => {
1166-
const expectedCell = expectedRow ? expectedRow[cellIndex] : null;
1166+
const expectedCell = expectedRow[cellIndex];
11671167
checkCellContent(actualCell, expectedCell);
11681168
});
11691169
});

0 commit comments

Comments
 (0)