【发布时间】:2019-10-15 15:58:36
【问题描述】:
我想创建一个具有动态键列表的接口,这些键基于枚举的值。
基本上,它看起来像这样:
enum MyEnum {
foo = "foo",
bar ="bar"
}
interface MyInterface { //A generic on this line
id: string;
objects: {
[key: string] : string; //Instead of mapping to string,
// map to the values of the enum
}
}
const x : MyInterface<MyEnum> = {
id: "123",
objects: {
foo: "hello",
bar: "world",
}
}
//Now I can access the values with:
console.log(x.objects[MyEnum.foo]);
const y : MyInterface<MyEnum> = {
id: "123",
objects: {
foo: "hello", //Should give typeScript warning - bar doesn't exist.
}
}
这里有两件事我不知道该怎么做。
- 如何定义泛型必须是枚举类型?
- 如何为通用枚举创建动态键列表?
如果有一个方便的解决方案可以做到这一点,我很高兴不专门使用枚举。
相关阅读:
这个 Typescript github 问题:https://github.com/microsoft/TypeScript/issues/13042
如果答案是 - “这是不可能的......”您能否提供一个指向最佳 Github 问题讨论的链接 - 以及解决方案的摘要?从我看到的阅读来看,这是很多人想要的功能。
更新:我目前的最佳解决方案包括创建一个接口作为伪枚举定义,以及该枚举的实现。 Playground here。不过我不是很满意。
【问题讨论】:
标签: typescript generics enums