为什么不支持可派生的枚举?

时间:2019-11-18 21:30:26

标签: typescript

如何创建作为另一个枚举的子集的枚举?

在某些情况下,支持枚举是另一个Enum的子集(使用不同名称)会很方便,该枚举在运行时派生相同的值。

是否有更好的方法来支持这种情况?

TypeScript

enum Original {
    value = "value",
    other = "other"
}

enum Derived {
    value = Original.value
}

const test: Original = Derived.value;

生成的JavaScript

"use strict";
var Original;
(function (Original) {
    Original["value"] = "value";
    Original["other"] = "other";
})(Original || (Original = {}));
var Derived;
(function (Derived) {
    Derived["value"] = "value";
})(Derived || (Derived = {}));
const test = Derived.value;

如果不将静态常量分配给“派生”枚举,而是在运行时从“原始”派生该值,则会很方便。

可能性:

  • 替换:由于枚举是从现有枚举中派生的,因此编译可以用原始值替换派生值。即无需在JavaScript中创建单独的“派生”对象。

替换示例:

"use strict";
var Original;
(function (Original) {
    Original["value"] = "value";
    Original["other"] = "other";
})(Original || (Original = {}));

const test = Original.value;

1 个答案:

答案 0 :(得分:1)

最简单的方法似乎是通过使用联合定义enum的子类型:

enum Original {
    foo = "foo",
    bar = "bar",
    baz = "baz",
}

type Derived = Original.foo | Original.bar;

这不允许您编写Derived.foo。如果有必要,您可以编写一个辅助函数来创建表示枚举子类型的对象:

type ValueOf<T> = T[keyof T];

function enumSubtype<T, K extends keyof T>(e: T, keys: K[]): Pick<T, K> {
    const out = {} as Pick<T, K>;
    for (let k of keys) {
        out[k] = e[k];
    }
    return out;
}

const Derived = enumSubtype(Original, ['foo', 'bar']);
type Derived = ValueOf<typeof Derived>;

类型DerivedOriginal.foo | Original.bar

Playground Link