How to define enum type in Angular to not violating tslint typedef rule
Asked Answered
P

1

5

To be able to use enumerations in template, we write below codes in ts file.

in workflowProgress.ts

export enum WorkflowProgress
{
    cancelled = 0,
    inProgress,
    done
}

in component.ts

export class Component {
   WorkflowProgress = WorkflowProgress;
   x : WorkflowProgress = WorkflowProgress.done;
}

in template.html

<div *ngIf="x === WorkflowProgress.done">

and we already have tslint with typedef rule enabled. but tslint is nagging about this line WorkflowProgress = WorkflowProgress;

[tslint] expected member-variable-declaration: 'WorkflowProgress' to have a typedef (typedef)

I can disable the rule for by adding // tslint:disable-next-line:typedef but I was wondering if there is better way to do it?

Paralyze answered 12/6, 2018 at 13:36 Comment(0)
S
11

You can use typeof operator to "query" the type of enum:

WorkflowProgress: typeof WorkflowProgress = WorkflowProgress
Shoop answered 12/6, 2018 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.