Skip to content

Commit deb77e2

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 2bfbc9f commit deb77e2

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
@@ -43,9 +43,9 @@ describe('MatTable', () => {
4343
const data = fixture.componentInstance.dataSource!.data;
4444
expectTableToMatchContent(tableElement, [
4545
['Column A', 'Column B', 'Column C'],
46-
[data[0].a, data[0].b, data[0].c],
47-
[data[1].a, data[1].b, data[1].c],
48-
[data[2].a, data[2].b, data[2].c],
46+
[data[0].a, data[0].b, data[0].c] as string[],
47+
[data[1].a, data[1].b, data[1].c] as string[],
48+
[data[2].a, data[2].b, data[2].c] as string[],
4949
['fourth_row'],
5050
['Footer A', 'Footer B', 'Footer C'],
5151
]);
@@ -91,10 +91,10 @@ describe('MatTable', () => {
9191
const data = fixture.componentInstance.dataSource!.data;
9292
expectTableToMatchContent(tableElement, [
9393
['Column A', 'Column B', 'Column C'],
94-
[data[0].a, data[0].b, data[0].c],
95-
[data[1].a, data[1].b, data[1].c],
96-
[data[2].a, data[2].b, data[2].c],
97-
[data[3].a, data[3].b, data[3].c],
94+
[data[0].a, data[0].b, data[0].c] as string[],
95+
[data[1].a, data[1].b, data[1].c] as string[],
96+
[data[2].a, data[2].b, data[2].c] as string[],
97+
[data[3].a, data[3].b, data[3].c] as string[],
9898
]);
9999
});
100100

@@ -186,9 +186,9 @@ describe('MatTable', () => {
186186
const data = fixture.componentInstance.dataSource!.data;
187187
expectTableToMatchContent(tableElement, [
188188
['Column A', 'Column B', 'Column C'],
189-
[data[0].a, data[0].b, data[0].c],
190-
[data[1].a, data[1].b, data[1].c],
191-
[data[2].a, data[2].b, data[2].c],
189+
[data[0].a, data[0].b, data[0].c] as string[],
190+
[data[1].a, data[1].b, data[1].c] as string[],
191+
[data[2].a, data[2].b, data[2].c] as string[],
192192
]);
193193
});
194194

@@ -200,9 +200,9 @@ describe('MatTable', () => {
200200
const data = fixture.componentInstance.dataSource!.data;
201201
expectTableToMatchContent(tableElement, [
202202
['Column A', 'Column B', 'Column C'],
203-
[data[0].a, data[0].b, data[0].c],
204-
[data[1].a, data[1].b, data[1].c],
205-
[data[2].a, data[2].b, data[2].c],
203+
[data[0].a, data[0].b, data[0].c] as string[],
204+
[data[1].a, data[1].b, data[1].c] as string[],
205+
[data[2].a, data[2].b, data[2].c] as string[],
206206
]);
207207
});
208208

@@ -384,7 +384,7 @@ describe('MatTable', () => {
384384
]);
385385

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

634-
dataSource.data = {} as any;
634+
dataSource.data = {} as TestData[];
635635
fixture.changeDetectorRef.markForCheck();
636636
fixture.detectChanges();
637637
expectTableToMatchContent(tableElement, [
@@ -1135,7 +1135,7 @@ function getActualTableContent(tableElement: Element): string[][] {
11351135
return actualTableContent.map(row => row.map(cell => cell.textContent!.trim()));
11361136
}
11371137

1138-
export function expectTableToMatchContent(tableElement: Element, expected: any[]) {
1138+
export function expectTableToMatchContent(tableElement: Element, expected: string[][]) {
11391139
const missedExpectations: string[] = [];
11401140
function checkCellContent(actualCell: string, expectedCell: string) {
11411141
if (actualCell !== expectedCell) {
@@ -1161,7 +1161,7 @@ export function expectTableToMatchContent(tableElement: Element, expected: any[]
11611161
}
11621162

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

0 commit comments

Comments
 (0)