You can define your two overloads and then specify the logic in a single function. Something like below should work.
function myfunc(...values : Array<number>): number;
function myfunc(values : Array<number>): number;
function myfunc(value : (Array<number> | number), ...values : Array<number>): number {
if (typeof value === 'number') {
values.unshift(value);
} else if (value === undefined) {
values = [];
} else {
values = value;
}
// logic
return values.length;
}
myfunc(); // correct
myfunc(1); // correct
myfunc(1, 2); // correct
myfunc([1, 2]); // correct
myfunc(1, 2, 3, 4); // correct
myfunc([1, 2, 3]); // correct
// myfunc([1, 2, 3, 4], 5, 6); // error
// myfunc([1, 2], [3, 4]); // error
console.log(myfunc(1, 2, 3)) // 3
console.log(myfunc([1, 2, 3])) // 3
Notice how because we have not defined an interface for an array followed by values, the final example results in an error which I think is the expected behaviour. An example can be seen here. We have to check for undefined here as the transpiled JavaScript does not take into account the different function overloads, and instead provides a single implementation where value
can be undefined.