Home > Blockchain >  Convert SortDirection to string - error TS2322
Convert SortDirection to string - error TS2322

Time:03-16

StrictTemplate is complaining that a SortDirection is not a string. However, an enum is a string, isn't it?

Type 'string' is not assignable to type 'SortDirection'.

<mat-table matSort matSortDirection="{{ this.sort.direction }}" ...

The definition of SortDirection is:

export declare type SortDirection = 'asc' | 'desc' | '';

Is there a way to convert it to string?

I tried matSortDirection="{{(string)this.sort.direction}}" and matSortDirection="{{this.sort.direction.toString()}}" but fail.

What's the best way to convert it?

CodePudding user response:

You can try to add helper $any to say to compiler, that nothing is wrong here and everything is correct

<mat-table matSort matSortDirection="{{ $any(this.sort.direction) }}" ...

  • Related