In this scenario, I have encapsulated three functions within a literal block:
type AnyCallback = (...args: any) => unknown;
const callbackAlpha = (data: string) => {
console.log(data);
return data.length;
};
const callbackBeta = (data: string, prefix: string) => {
console.log(data, prefix);
return prefix + data;
};
const callbackGamma = (data: string, prefix: string, suffix: string) => {
console.log(data, prefix, suffix);
return prefix + data + suffix;
};
let allCallBack: Record<string, AnyCallback> = {
callbackAlpha,
callbackBeta,
callbackGamma,
};
Next, the goal is to establish an array type that will record the function name and parameters when called. The structure of each member should look like this:
{
name: //name of a function in the literal
args: //parameters that needed when calling the function
}
Initially, this approach was attempted:
type TaskType = keyof typeof allCallBack;
type TaskQueue = {
name: TaskType;
args: Parameters<(typeof allCallBack)[TaskType]>;
}[];
This method turned out to be incorrect since it allows arbitrary values for "name" and accepts any parameters for the three functions.