Advertisements
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???
Anyways, yeah, you can use a default value to show that it’s optional.