diff --git a/src/cdk/table/table.spec.ts b/src/cdk/table/table.spec.ts index d618b79523e6..b76b33a0a368 100644 --- a/src/cdk/table/table.spec.ts +++ b/src/cdk/table/table.spec.ts @@ -688,6 +688,19 @@ describe('CdkTable', () => { ); }); + it('should throw a descriptive error (not a raw TypeError) for an unknown column in production mode', () => { + const originalNgDevMode = (globalThis as any).ngDevMode; + (globalThis as any).ngDevMode = false; + + try { + expect(() => + TestBed.createComponent(MissingColumnDefCdkTableApp).detectChanges(), + ).toThrowError(getTableUnknownColumnError('column_a').message); + } finally { + (globalThis as any).ngDevMode = originalNgDevMode; + } + }); + it('should pick up columns that are indirect descendants', () => { expect(() => TestBed.createComponent(TableWithIndirectDescendantDefs).detectChanges(), diff --git a/src/cdk/table/table.ts b/src/cdk/table/table.ts index 0818d1ef981b..9eb79e193627 100644 --- a/src/cdk/table/table.ts +++ b/src/cdk/table/table.ts @@ -1287,10 +1287,10 @@ export class CdkTable private _addStickyColumnStyles(rows: HTMLElement[], rowDef: BaseRowDef) { const columnDefs = Array.from(rowDef?.columns || []).map(columnName => { const columnDef = this._columnDefsByName.get(columnName); - if (!columnDef && (typeof ngDevMode === 'undefined' || ngDevMode)) { + if (!columnDef) { throw getTableUnknownColumnError(columnName); } - return columnDef!; + return columnDef; }); const stickyStartStates = columnDefs.map(columnDef => columnDef.sticky); const stickyEndStates = columnDefs.map(columnDef => columnDef.stickyEnd); @@ -1415,11 +1415,11 @@ export class CdkTable return Array.from(rowDef.columns, columnId => { const column = this._columnDefsByName.get(columnId); - if (!column && (typeof ngDevMode === 'undefined' || ngDevMode)) { + if (!column) { throw getTableUnknownColumnError(columnId); } - return rowDef.extractCellTemplate(column!); + return rowDef.extractCellTemplate(column); }); }