Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading