Imagine having a basic enum
enum MyEnum {
a,
b,
c
}
Converting the enum into key-value pairs is straightforward:
type A<V> = { [k in MyEnum]: V };
const testA: A<string> = {
[MyEnum.a]: '',
[MyEnum.b]: '',
[MyEnum.c]: ''
};
The issue arises when attempting to use the enum as a generic type:
type B1<T, V> = { [k in T]: V } // this won't work
const testB: A<MyEnum , string> = { ... } // example
I've tried various approaches on this play-ground
There are some related questions (listed below) but I still believe that if the first option (
type A<V> = { [k in MyEnum]: V };
) works, the other options should as well (type B1<T, V> =
).
Mapping Enum to Type of keys or values
is-it-possible-to-allow-literal-string-values-with-typescripts-enum-type