Expected 2 arguments but got 1 – but I want to pass just one

I created a function with this signature:

  const createSomething = (
    someRange: number[],
    { option }: { option?: boolean }
  ) =>...

Sometimes I pass to the function just the someRange argument, and sometime the additional object argument. However I receive an error that the function expects two arguments. How can I declare the second argument to be optional?

>Solution :

It’s quite odd that you can’t do:

const createSomething = (
    someRange: number[],
    { option }?: { option?: boolean } // INVALID
) => {}

so you have to do:

const createSomething = (
    someRange: number[],
    { option }: { option?: boolean } = {}
) => {}

but then when you hover over createSomething, it shows the signature as the former???

odd behavior

Anyways, yeah, you can use a default value to show that it’s optional.

Leave a Reply