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

半腔热情 提交于 2020-06-08 19:17:11

问题


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?


回答1:


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

WorkflowProgress: typeof WorkflowProgress = WorkflowProgress


来源:https://stackoverflow.com/questions/50818606/how-to-define-enum-type-in-angular-to-not-violating-tslint-typedef-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!