There is a function that I have defined in the following way:
function getRangeBounds(start: number, stop?: number, step?: number) {
if (step === undefined) step = 1;
const actualStart = start !== undefined && stop !== undefined ? start : 0;
const actualStop = stop === undefined ? start : stop;
return [actualStart, actualStop, step];
}
I aim to ensure that this function's arguments are typed and can be reused. So, I created the following:
type Range = Parameters<typeof getRangeBounds>;
I plan for the type Range
to be reusable in a few more functions, such as range
and mapRange
, which will have the following signatures respectively:
export function range(...args: Range): ReadonlyArray<number> {
/* This will invoke getRangeBounds at some point */
}
export function mapRange(rangeArgs: Range, mapper: MapFunc<number>) {
return range(...rangeArgs).map(mapper);
}
How can I ensure that these functions adhere to the contract of Range
and also allow for additional arguments in certain functions like the modified mapRange
above?
Edit: I have resolved the compilation error mentioned earlier, so the revised version of mapRange
appears as follows:
export function mapRange(rangeArgs: Range, mapper: MapFunc<number>) {
return range(...rangeArgs).map(mapper);
}
However, now I need to call the function in this manner:
mapRange([start, stop, step])