I have (in TypeScript) a class called 'Instructions', which has a static method called 'operate'. This method accepts two certain arguments, and two more possible ones. The first argument is a 'callback function' that the 'operate' method should call, and the rest of the arguments are the same arguments that will be passed to the callback function I got as the first argument. My question is, how to define the type of the callback function.
At first, I thought of implementing it like this:
interface Operand {
// ...
}
interface OperationFunc {
(dstOperand: Operand, srcOperand?: Operand, powerEvaluation?: number): void;
}
class Instructions {
static operate (callback: OperationFunc, dstOperand, srcOperand?, powerEvaluation?) {
callback(dstOperand, srcOperand, powerEvaluation);
}
}
Which turned out to be unsuccessful for several reasons. The main reason is that the callback function is not the same function in every call. But - these are different functions, and each time I pass a different callback function as needed. Sometimes it is a function that receives a single 'Operand' type argument, sometimes it is a function that receives two 'Operand' type arguments, and sometimes it is a function that receives three arguments - Two of them are of the 'Operand' type, and the third of the 'number' type. These three type of functions return void.
interface Operand {
// ...
}
type Function1 = (dst: Operand) => void; // The first type of callback functions
type Function2 = (dst: Operand, src: Operand) => void; // The second type of callback functions
type Function3 = (dst: Operand, src: Operand, powerEvaluation: number) => void; // The third type of callback functions
Does anyone have any idea how to define a Type of such a callback function?