You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(table): typescript/strong typing of cell templates #22290
As we've been converting more code to use Material Data Tables, the lack of strong typing has been really problematic, and it feels like a downgrade in developer experience compared to regular tables with *ngFor.
Further motivation for this has been described in:
There's a bit of redundancy, with needing to specify the data source in every *matCellDef that you want type checked, but it's optional. If it isn't defined, it falls back to any just like the original directive.
Reacted by KingDarBoja, Enea Jahollari, Lars Berger, Dirk Luijk, Michał Stawicki, Ivan Jiang, aceArt-GmbH, nbitouze, Daniel Kimmich, Alexis and 13 more
Reacted by aceArt-GmbH, Olufemi Oyewole, rvlajcev, sjenderpender and Alexandru Barascu
Install the latest extension in the VSCode Marketplace
Set strictTemplates: true in tsconfig.json at angularCompilerOptions. This might cause diagnostics to appear if project is not strictTemplates compatible (pretty much strict mode is a must).
Open an Angular project in Visual Studio Code.
Go to Preferences -> Settings -> Extensions -> Angular Language Service -> Check “Experimental Ivy”.
Still waiting for this feature to be integrated in Angular as loosing types in table is still a big disadvantage.
Reacted by Adrien Crivelli, aceArt-GmbH, Aleksa Robavs, openminded-oscar, hlovdal, Francesco Moro, Christopher Baker, Natalia LaFever, keatkeat87, cmartos-hogan and 27 more
Reacted by aceArt-GmbH, Bastian Bense, ionut-gheorghe and Ahmed Ben Chakhter
Finds lots of real type errors.
But also shows many limitations of angular templates. Like optional chaining working different from typescript, numeric index signature ([key: `prop${number}`]) and type narrowing
Reacted by Max Kokosha, Markus Hartmair, RebendaJiri, nolegendhere, Aurélien Petit, Eric Neher, ManicardiFrancesco, Nicolas, Alex88360, Konstantin Shutkin and 26 more
Reacted by aceArt-GmbH, Alessandro Caramia, Ahmed Ben Chakhter and Dorian
I rebuilt the quick fix of @andreialecu and @nartc as a CdkCellDef version instead of MatCellDef for users of CdkTable instead of MatTable
import{CdkCellDef,DataSource}from'@angular/cdk/table';import{Directive,Input}from'@angular/core';import{Observable}from'rxjs';/** * IMPORTANT: as soon as https://github.com/angular/components/issues/22290 is solved, this directive can be deleted * * its only purpose is to make element type safe in `<td cdk-cell *cdkCellDef="let element">…<\td>` when using cdk table. */
@Directive({standalone: true,// eslint-disable-next-line @angular-eslint/directive-selectorselector: '[cdkCellDef]',// same selector as CdkCellDef to override by force for type-safetyproviders: [{provide: CdkCellDef,useExisting: TypeSafeCdkCellDef}],// overwrite CdkCellDef with this typed version of it})exportclassTypeSafeCdkCellDef<T>extendsCdkCellDef{
@Input({required: true})publiccdkCellDefDataSource: T[]|Observable<T[]|DataSource<T>>=[];publicstaticngTemplateContextGuard<T>(dir: TypeSafeCdkCellDef<T>,ctx: unknown): ctx is {$implicit: T;index: number}{returntrue;}}
You can add this directive to your providers and then use it like:
There's a bit of redundancy, with needing to specify the data source in every *cdkCellDef that you want type checked, but it's optional. If it isn't defined, it falls back to any just like the original directive.
Reacted by Satyam Kumar, Max Kokosha, Maarten Tibau and Andreas Dorner
Activity
andreialecu commentedon Mar 19, 2021
Very simple repro:
Generates the following typecheck block with Ivy:
The correct type is available as part of the
_t2variable.If
_t4was able to use it instead ofany, it seems like this would be achievable.andreialecu commentedon Mar 19, 2021
Here's a working workaround for this. Most of the credits for it go to @nartc 🏆 , on the Angular Discord.
You can add this directive to your
providersand then use it like:And

elementwill no longer beany:There's a bit of redundancy, with needing to specify the data source in every
*matCellDefthat you want type checked, but it's optional. If it isn't defined, it falls back toanyjust like the original directive.KingDarBoja commentedon Mar 22, 2021
Do not forget to follow the steps at Under the hood of the language service to properly enable this workaround.
strictTemplates: truein tsconfig.json atangularCompilerOptions. This might cause diagnostics to appear if project is not strictTemplates compatible (pretty much strict mode is a must).aceArt-GmbH commentedon Jan 5, 2023
Is there a solution which would only require 1 change per table instead of 1 change per column?
wongk commentedon Apr 17, 2023
+1
DmitryEfimenko commentedon May 21, 2023
I feel like this issue belongs under @angular/core instead of @angular/components. The fix should allow providing type through a parent directive.
Maybe it would work if
ngTemplateContextGuardaccepted 3 arguments?Simon-GHI commentedon Jan 16, 2024
Still waiting for this feature to be integrated in Angular as loosing types in table is still a big disadvantage.
maartentibau commentedon Jul 17, 2024
Would love to get this one as well... 🙏
aceArt-GmbH commentedon Jul 25, 2024
For simply using the table reference version you can search-replace with following regex
And
mat-table->mat-table #table.Finds lots of real type errors.
But also shows many limitations of angular templates. Like optional chaining working different from typescript, numeric index signature (
[key: `prop${number}`]) and type narrowingmelroy89 commentedon Mar 24, 2025
We soon have GTA 6 before
matCellDefis fixed.ahmad-ben commentedon Jul 16, 2025
Very needed feature...
DorianNaaji commentedon Aug 12, 2025
Not having types is a real nightmare, and the existing workarounds are not quite satisfying in my humble opinion...
Definitely waiting for this one 😭
New `TypedMatCellDef` #12186
marfrede commentedon May 12, 2026
I rebuilt the quick fix of @andreialecu and @nartc as a
CdkCellDefversion instead ofMatCellDeffor users ofCdkTableinstead ofMatTableYou can add this directive to your
providersand then use it like:And
elementwill no longer beany:There's a bit of redundancy, with needing to specify the data source in every
*cdkCellDefthat you want type checked, but it's optional. If it isn't defined, it falls back toanyjust like the original directive.