I'd like to write a function that takes an object argument, uses destructuring in the function signature, and have that argument be optional:
myFunction({opt1, opt2}?: {opt1?: boolean, opt2?: boolean})
However, Typescript doesn't let me ("A binding pattern parameter cannot be optional in an implementation signature.").
Of course I could do it if I didn't destructure:
myFunction(options?: {opt1?: boolean, opt2?: boolean}) {
const opt1 = options.opt1;
const opt2 = options.opt1;
...
It seems like these should be the same thing, yet the top example is not allowed.
I'd like to use a destructured syntax (1) because it exists, and is a nice syntax, and it seems natural that the two functions above should act the same, and (2) because I also want a concise way to specify defaults:
myFunction({opt1, opt2 = true}?: {opt1?: boolean, opt2?: boolean})
Without destructuring, I have to bury these defaults in the implementation of the function, or have an argument that is actually some class with a constructor...