Is there a way to create a function that accepts an optional object argument using destructuring in Typescript?
myFunction({opt1, opt2}?: {opt1?: boolean, opt2?: boolean})
The error message "A binding pattern parameter cannot be optional in an implementation signature" prevents this from working.
One workaround is to forego destructuring:
myFunction(options?: {opt1?: boolean, opt2?: boolean}) {
const opt1 = options.opt1;
const opt2 = options.opt1;
...
Despite their similarity, the two methods are not interchangeable.
The desire to use a destructured syntax stems from its convenience and natural flow. It also allows for concise default values:
myFunction({opt1, opt2 = true}?: {opt1?: boolean, opt2?: boolean})
If destructuring is not used, default values become obscured within the function implementation or require a more complex approach involving classes.