简体   繁体   English

打字稿枚举值作为数组参数

[英]Typescript enum value as array parameter

I will first of all state, that I am using TypeScript2.4, so I can use string ENUMs, ala我首先声明,我使用的是 TypeScript2.4,所以我可以使用字符串 ENUM,ala

export enum PermissionScope {
BRAND = "brand",
IMAGE_MARKER = "image-marker",
COMPANY = "company",
PRODUCT = "product",
FINANCIALS = "financials"
}

I don't know how to do 2 things.我不知道如何做两件事。

  1. How can I type guard an enum, say我如何输入保护枚举,说

    class A { let a : PermissionScope; }

now when I try to instantiate this variable by现在,当我尝试通过以下方式实例化此变量时

A.a = PermissionScope.BRAND

I get an error我收到一个错误

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

This can kindof be fixed by这可以通过以下方式解决

A.a = <PermissionScope> PermissionScope.BRAND

But I'm unsure if this is the right solution because as I understand, the underlying problem is that the variable is expected to be of a PermissionScope Enum Object type, not of a valid Enum value from PermissionScope.但我不确定这是否是正确的解决方案,因为据我所知,潜在的问题是该变量应该是 PermissionScope 枚举对象类型,而不是来自 PermissionScope 的有效枚举值。

How can I set the variable type so that it can only be one of the types from the enum?如何设置变量类型,使其只能是枚举中的一种类型?

The second question is.第二个问题是。

  1. What if I want to ask if a string is a valid Enum value, aka如果我想询问一个字符串是否是一个有效的 Enum 值,又名

    var a = "brand"; //true var b = "candy crush" //false

For the first question, what you're trying to do shouldn't be a problem.对于第一个问题,您尝试做的事情应该不成问题。
The 'let' inside your class may be causing your issues, I changed that to public and had no issues.您班级中的“让”可能会导致您的问题,我将其更改为 public 并且没有问题。
I also assume that your Aa example is meant to be an instance of A?我还假设您的 Aa 示例是 A 的一个实例? your variable 'a' is an instance variable.您的变量 'a' 是一个实例变量。

class A {
    public a : PermissionScope = PermissionScope.BRAND;
}
const instance = new A();

instance.a = PermissionScope.BRAND;

Answering your second question, the enum gets converted to an object under the hood.回答你的第二个问题,枚举被转换为引擎盖下的对象。 Which looks like this in raw js在原始js中看起来像这样

var PermissionScope;
(function (PermissionScope) {
    PermissionScope["BRAND"] = "brand";
    PermissionScope["IMAGE_MARKER"] = "image-marker";
    PermissionScope["COMPANY"] = "company";
    PermissionScope["PRODUCT"] = "product";
    PermissionScope["FINANCIALS"] = "financials";
})(PermissionScope = exports.PermissionScope || (exports.PermissionScope = {}));

So you can see if the static part of your enum is valid using 'in'因此,您可以使用“in”查看枚举的静态部分是否有效

console.log('BRAND' in PermissionScope); //true
console.log('IMAGE_MARKER' in PermissionScope); //true
console.log('ASDF' in PermissionScope); //false

If you want to check the value part of the enum you would have to iterate through the object values and check that如果要检查枚举的值部分,则必须遍历对象值并检查

function isValidEnumValue(enumType: any, value: string) {
    //NOTE: Object.values is only available in ES7 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values)
    return Object.values(enumType).indexOf(value) !== -1;
}

console.log(isValidEnumValue(PermissionScope, 'brand')); //true
console.log(isValidEnumValue(PermissionScope, 'asdf')); //false

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

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