简体   繁体   English

将枚举类型转换为 Typescript 的正确方法

[英]Proper way to cast an enum to Type in Typescript

I have an angular enum like this:我有一个像这样的 angular enum

export enum ProtectiveClassificationType
{
  InflationAdjustedNominal =1,
  NominalInflationAdjusted =2,
  InflationAdjusted =3,
  Nominal =4,
  None = 5
}

and 2 methods.和2种方法。 One to get the description of an enum item and the other one to return the enum as a <key, value> pair to use it into select lists:一个获取枚举项的描述,另一个将枚举作为 <key, value> 对返回以将其用于 select 列表:

export interface SelectPayload {
 key: string | number;
 value: string;
}

  protected enumToList(type: Type, nameResolver: (element: number) => string): SelectPayload[] {
        const retList: SelectPayload[] = [];
        const values = Object.values(type);
        for (const value of values.slice(values.length / 2))
        retList.push({ key: value, value: nameResolver(value) });
        return retList;
    }//enumToList


  public getProtectiveClassificationByType(type: number): string {
    switch (type) {
      case ProtectiveClassificationType.InflationAdjusted: return "Inflation Adjusted";
      case ProtectiveClassificationType.InflationAdjustedNominal: return "Inflation Adjusted then Nominal";
      case ProtectiveClassificationType.Nominal: return "Nominal";
      case ProtectiveClassificationType.NominalInflationAdjusted: return "Nominal then Inflation Adjusted";
      case ProtectiveClassificationType.None: return "None";
      default: return "Unknown";
    }
  }

  public  getProtectiveClassificationTypeToList(): SelectPayload[] {       
    return this.enumToList(ProtectiveClassificationType as unknown as Type, this.getProtectiveClassificationByType.bind(this));      
  }//getProtectiveClassificationTypeToList

The method enumToList is from a base class and in order to use it I have to do such a cast ProtectiveClassificationType as unknown as Type .方法enumToList来自基础 class ,为了使用它,我必须执行ProtectiveClassificationType as unknown as Type For me it doesn't look right and now my question is if there is a better way to do it.对我来说,它看起来不正确,现在我的问题是是否有更好的方法来做到这一点。 PS: "Type" is from "import { Type } from "typescript"; PS: "Type" 来自 "import { Type } from "typescript";

Explanation解释

Your solution is the only one that will work without making changes to enumToList function, because import { Type } from "typescript" doesn't overlap with enum .您的解决方案是唯一可以在不更改enumToList function 的情况下工作的解决方案,因为import { Type } from "typescript"不与enum重叠。

ProtectiveClassificationType as unknown as Type

Refactoring Solution重构解决方案

This involves changing the enumToList method to use generics, then later when you use the function, don't specify the type in the generics, however your linter may complain.这涉及将 enumToList 方法更改为使用 generics,然后当您使用 function 时,不要在 generics 中指定类型,但是您的 linter 可能会抱怨。 Although, this solution is the equivalent to using any which removes the type safety but then again, so does forcing the type of the enum to be Type when they don't overlap.虽然,此解决方案等效于使用any删除类型安全的方法,但同样如此,当它们不重叠时,强制枚举的类型为Type也是如此。

protected enumToList<T>(enum: T, nameResolver: (element: number) => string): SelectPayload[] {
      const retList: SelectPayload[] = [];
      const values = Object.values(enum);
      for (const value of values.slice(values.length / 2))
      retList.push({ key: value, value: nameResolver(value) });
      return retList;
  }//enumToList

...

public getProtectiveClassificationTypeToList(): SelectPayload[] {       
  return this.enumToList(ProtectiveClassificationType, this.getProtectiveClassificationByType.bind(this));      
}//getProtectiveClassificationTypeToList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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