Skip to content

[Table] Mat cell definitions do not ensure correct typing #16273

@earshinov

Description

@earshinov

Cell definitions like

<td mat-cell *matCellDef="let element"> {{element.position}} </td>

do not enforce typing. Even if table dataSource

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

is properly typed, let element is still of type any, which does not allow the TypeScript compiler to type-check the markup inside the cell definition.

On the test page (https://stackblitz.com/edit/mat-tables-typing), I modified the table-basic example from the repo to output a non-existing field (element.non_existing_field), and the project is still compiled without any TypeScript errors.

I am sure I does not need to explain how beneficial strong typing is for early detection of programming errors. Furthermore, TypeScript and strong typing is one of the principal features of Angular as a framework, so I consider it great shame that this feature is not properly leveraged here.

I am not an expert in Angular, so please let me know if there are some Angular limitations that prevent Angular Material from providing proper typing support. I will appreciate if you point me to an appropriate ticket in Angular project I could watch.

Reproduction

https://stackblitz.com/edit/mat-tables-typing

Expected Behavior

In a cell definition like

<td mat-cell *matCellDef="let element"> {{element.position}} </td>

the element variable should receive a TypeScript type derived from the type of dataSource input passed to mat-table.

Changing the cell definition into

<td mat-cell *matCellDef="let element"> {{element.non_existing_field}} </td>

should cause TypeScript errors to be emitted when the project is AOT-compiled.

Actual Behavior

The element variable receives type any.

Changing the cell definition into

<td mat-cell *matCellDef="let element"> {{element.non_existing_field}} </td>

does not trigger any TypeScript errors. I believe, you will have to download the StackBlitz project locally in order to check this.

Environment

  • Angular: 8.0.0
  • CDK/Material: 8.0.0
  • Browser(s): Google Chrome | 74.0.3729.169 (Official Build) (64-bit)
  • Operating System: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions