Typescript 将枚举用作泛型类型
我正在尝试实现如下功能:Typescript 将枚举用作泛型类型,typescript,Typescript,我正在尝试实现如下功能: enum MyEnum { ONE = "ONE", TWO = "TWO" } interface MyInterface<T extends enum> { //Obviously wrong syntax value: T; //Use a single value of the enum values: Record<T, number>; //Use all of the keys of the enum
enum MyEnum {
ONE = "ONE",
TWO = "TWO"
}
interface MyInterface<T extends enum> { //Obviously wrong syntax
value: T; //Use a single value of the enum
values: Record<T, number>; //Use all of the keys of the enum
optionalValues: Record<T?, number>; //Use only the keys of the enum, but not necessarily all of them.
}
const a : MyInterface<MyEnum> = {
value: "ONE", //OK
values: {
ONE: 1, //OK
TWO: 2,
THREE: 3 //NOT OK
},
optionalValues: {
ONE: 111, //OK
THREE: 3 //NOT OK
}
}
const b : MyInterface<MyEnum> = {
value: MyEnum.ONE, //OK
values: {
ONE: 1, //Not ok - not all enum values used
},
optionalValues: {
[MyEnum.ONE]: 111, //Ok, and generally this is the way I want to be using this.
}
}
enum MyEnum{
ONE=“ONE”,
TWO=“TWO”
}
接口MyInterface{//显然语法错误
value:T;//使用枚举的单个值
value:Record;//使用枚举的所有键
optionalValues:Record;//仅使用枚举的键,但不一定全部使用。
}
常量a:MyInterface={
值:“一”//OK
价值观:{
一:1,//好的
二:二,,
三:3/不好
},
可选值:{
一:111,//好的
三:3/不好
}
}
常量b:MyInterface={
值:MyEnum.ONE,//确定
价值观:{
一:1,//不正常-未使用所有枚举值
},
可选值:{
[MyEnum.ONE]:111,//好的,一般来说,这就是我想要使用它的方式。
}
}
也就是说,我希望能够使用枚举作为指定键列表的一种方式,然后将接口定义为包含这些键的对象
即
const冰淇淋=MyInterface={
...
}
常数fruit=MyInterface={
...
}
我怎样才能做到这一点?这似乎是一个相当常见的用例 没有
enum
约束。枚举可以是string
或number
,因此string | number
的约束是我们能做的最好的:
interface MyInterface<T extends string | number> {
value: T; //Use a single value of the enum
values: Record<T, number>; //Use all of the keys of the enum
optionalValues: Partial<Record<T, number>>; //Use only the keys of the enum, but not necessarily all of them.
}
enum MyEnum {
ONE = "ONE",
TWO = "TWO"
}
const a : MyInterface<MyEnum> = {
value: MyEnum.ONE, //OK
values: {
[MyEnum.ONE]: 1, //OK
[MyEnum.TWO]: 2,
[MyEnum.THREE]: 3 //NOT OK
},
optionalValues: {
[MyEnum.ONE]: 111, //OK
[MyEnum.THREE]: 3 //NOT OK
}
}
接口MyInterface{
value:T;//使用枚举的单个值
value:Record;//使用枚举的所有键
optionalValues:Partial;//仅使用枚举的键,但不一定全部使用。
}
髓鞘{
ONE=“ONE”,
TWO=“TWO”
}
常量a:MyInterface={
值:MyEnum.ONE,//确定
价值观:{
[MyEnum.ONE]:1,//好的
[MyEnum.2]:2,
[MyEnum.THREE]:3//不正常
},
可选值:{
[MyEnum.ONE]:111,//好的
[MyEnum.THREE]:3//不正常
}
}
我们也可以使用枚举成员名,但必须传入枚举容器对象的类型,而不是枚举的类型
interface MyInterface<T extends Record<keyof T, string | number>> { // enum like object
value: keyof T; //Use a single value of the enum
values: Record<keyof T, number>; //Use all of the keys of the enum
optionalValues: Partial<Record<keyof T, number>>; //Use only the keys of the enum, but not necessarily all of them.
}
enum MyEnum {
ONE = "ONE",
TWO = "TWO"
}
const a : MyInterface<typeof MyEnum> = {
value: "ONE", //OK
values: {
"ONE": 1, //OK
"TWO": 2,
"THREE": 3 //NOT OK
},
optionalValues: {
"ONE": 111, //OK
"THREE": 3 //NOT OK
}
}
interface MyInterface{//enum-like对象
value:keyof T;//使用枚举的单个值
value:Record;//使用枚举的所有键
optionalValues:Partial;//仅使用枚举的键,但不一定全部使用。
}
髓鞘{
ONE=“ONE”,
TWO=“TWO”
}
常量a:MyInterface={
值:“一”//OK
价值观:{
“一”:1,//好的
"二":,
“三”:3/不好
},
可选值:{
“一”:111,//好的
“三”:3/不好
}
}
为什么要这样做?如果您只是定义接口,keyof
不提供枚举的功能吗?恐怕我一点也不了解。我不清楚,你想用[MyEnum.ONE]
还是“ONE”
作为键?两者都可以,但如果要确保指定所有成员,则不能混合使用。此外,也没有enum
约束,我们能做的最好的事情是将constrarin添加到具有number
或string
值的对象,如果您愿意,我可以提供一个参考(我知道有一个GH问题,我只需要搜索它)@TitianCernicova Dragomir-理想情况下我可以使用“一”,虽然如果我不得不使用方括号,我不会感到痛苦。这就是github的问题吗@德约翰斯顿这就是我想的
interface MyInterface<T extends Record<keyof T, string | number>> { // enum like object
value: keyof T; //Use a single value of the enum
values: Record<keyof T, number>; //Use all of the keys of the enum
optionalValues: Partial<Record<keyof T, number>>; //Use only the keys of the enum, but not necessarily all of them.
}
enum MyEnum {
ONE = "ONE",
TWO = "TWO"
}
const a : MyInterface<typeof MyEnum> = {
value: "ONE", //OK
values: {
"ONE": 1, //OK
"TWO": 2,
"THREE": 3 //NOT OK
},
optionalValues: {
"ONE": 111, //OK
"THREE": 3 //NOT OK
}
}