After spending a while pondering over this issue, I have yet to discover an elegant solution.
My dilemma begins with an Enum:
export enum Enum {
A,
B,
C,
}
Next, there is an object that utilizes the Enum. While the structure is somewhat predictable in that its value will always be a function returning a string, the uncertainty lies in the fact that the function may accept any number of arguments:
import { A, B, C } from '../enums/';
export const obj = {
[A]: () => 'example 1',
[B]: (arg1: string) => 'example 2, arg1: %s',
[C]: (nr: string, arg2: string) => 'example %s %s',
}
The final piece of the puzzle is a function which leverages obj
to fetch specific strings based on its arguments:
const fct = <E extends Enum>(
code: E,
...args: Parameters<(typeof obj)[E]>
) => {
...
}
Although everything functions properly, it lacks type safety since every Enum value must be assigned to a value within obj
.
If I were to create a basic type, Parameter
would not accurately display the arguments of the function:
type ObjectType = { [key in Enum]: (...args: any[]) => string }
Parameters<(typeof obj)[A]> // -> (...args: any[])