简体   繁体   中英

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

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?

您可以使用typeof运算符来“查询”枚举的类型:

WorkflowProgress: typeof WorkflowProgress = WorkflowProgress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM