Reputation: 11803
I'm trying to achieve some functionality that looks like this:
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.
}
}
That is - I want to be able to use enums as a way to specify a list of keys, and then define interfaces as objects that contain those keys.
ie.
const iceCreams = MyInterface<IceCreamFlavours> = {
...
}
const fruit = MyInterface<FruitTypes> = {
...
}
How can I achieve this? It seems like a fairly common use case.
Upvotes: 2
Views: 378
Reputation: 249606
There is no enum
constraint. Enums can be either string
or number
, so a constraint of string | number
is the best we can do:
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
}
}
We can also use the enum member names but then we have to pass in the type for enum container object not the the type of the enum.
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
}
}
Upvotes: 2