Here are a couple of ways to achieve the API you asked about:
TS Playground link
class MyClass {
// the method in your question
static myFunc (
param1: string,
param2?: string,
param3?: string,
param4 = true,
): void {
console.log([param1, param2, param3, param4]);
}
// parameters reordered
static myRefactoredFunc (
param1: string,
param4 = true,
param2?: string,
param3?: string,
): void {
console.log([param1, param2, param3, param4]);
}
}
// if you can't modify the class, you can create a function with the
// parameters ordered by your preference, which calls the original method
// in the function body and returns its return value
const myFunc = (
param1: Parameters<typeof MyClass['myFunc']>[0],
param4?: Parameters<typeof MyClass['myFunc']>[3],
param2?: Parameters<typeof MyClass['myFunc']>[1],
param3?: Parameters<typeof MyClass['myFunc']>[2],
): ReturnType<typeof MyClass['myFunc']> => MyClass.myFunc(param1, param2, param3, param4);
// these are all equivalent
MyClass.myFunc('foo', undefined, undefined, false);
MyClass.myRefactoredFunc('foo', false);
myFunc('foo', false);