Is there a way to retrieve the union or enum type from a typeof
type in TypeScript?
For instance:
const myConfs: { [k: string]: (myArg: { name: string }) => string } = {
'Hello': ({ name }) => `World from ${name}`,
'Goodbye': ({ name }) => `World from ${name}`,
};
type MyKeys = keyof typeof myConfs;
// How can I make this throw an error?
const key: MyKeys = 'hello';
I attempted removing the type definition of myConfs
, which worked initially, but it caused issues with the callback argument's type definition within the value field of myConfs
.