0

I have the following function:

function myFunc(
  id: string,
  optionalParamOne?: number,
  optionalParamTwo?: string
) {
  console.log(optionalParamTwo);
}

I would like to call this function by specifying the id as it is required and optionalParamTwo. I don't care about optionalParamOne. I cannot change this function.

How can I call this function with the parameters id and optionalParamTwo without having to specify optionalParamOne?

I could do this:

myFunc('abc', null, 'dog')

But I have many optional parameters in my function signature and I'd rather not specify a value for each.

Demo

ViqMontana
  • 5,090
  • 3
  • 19
  • 54
  • I mentioned in my question _I cannot change this function_ – ViqMontana Mar 10 '22 at 14:38
  • 1
    If you can't change the function, then you have to pass `undefined` for all the optional values. Dont think there is a work around as we are limited to just calling the function – Tushar Gupta Mar 10 '22 at 14:41
  • 1
    If you cannot change the function, then I am afraid that you'll have to pass `undefined` or `null` to every parameter. Next time consider using an `object` parameter when there's 3 or more params, that way you won't have to care about the order. – Kapobajza Mar 10 '22 at 14:43
  • Does https://stackoverflow.com/questions/32518615/skip-arguments-in-a-javascript-function answer your question? – PIG208 Mar 10 '22 at 15:03

2 Answers2

1

You must provide some value (even if its undefined) for every ordered parameter before you can provide a value for a higher-indexed parameter.

The common pattern for reducing boilerplate when invoking functions with uninteresting parameters is to curry:

TS Playground

function myFunc (
  id: string,
  optionalParamOne?: number,
  optionalParamTwo?: string
) {
  console.log(optionalParamTwo);
}

// Create a parameter-expanding function:
const myParams = (id: string, optionalParamTwo?: string) => [id, undefined, optionalParamTwo] as const;
//                                                               ^^^^^^^^^
// Include undefined for the params you don't want to think about when calling the function

myFunc(...myParams('abc', 'dog')); // logs "dog"

// Or just curry it:
const myFuncWithFewerParams = (
  id: string,
  optionalParamTwo?: string,
) => myFunc(id, undefined, optionalParamTwo);

myFuncWithFewerParams('abc', 'cat'); // logs "cat"

For sliced tuples (addressing partial parameters) in the type system, see this answer.

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
0

It's not possible to do so without changing the function. You can write a wrapper around this function with destructuring assignment like so to achieve the desired result:

function myFunc(
  id: string,
  optionalParamOne?: number,
  optionalParamTwo?: string
) {
  console.log(optionalParamTwo);
}

interface MyFuncParams {
  optionalParamOne?: number,
  optionalParamTwo?: string
}

function myFuncWrapper(id: string, { optionalParamOne, optionalParamTwo }: MyFuncParams = {}) {
  myFunc(id, optionalParamOne, optionalParamTwo);
}

myFuncWrapper('abc', { optionalParamTwo: 'dog' });
mmmm
  • 16
  • 4