I am working with a string enum where each value is associated with a display name as shown below:
enum MyEnum {
key1 = 'one',
key2 = 'two',
key3 = 'three',
}
const myKey: MyEnum = 'two' as MyEnum; // The value for myKey is only known at runtime
const lookupResult = {
[MyEnum.key1]: 'display name for key1',
[MyEnum.key2]: 'display name for key2',
}[myKey];
However, I encountered an error when using the lookup object:
TS7017: Element implicitly has an 'any' type because type '{ [MyEnum.key1]: string; [MyEnum.key2]: string; }' has no index signature.
How can I successfully match myKey
to its display name without triggering this error?
Do you think it would be better not to use an enum in this scenario? I require both the enum values and their corresponding display names for different contexts.