Use a default parameter instead:
function myFunction({ opt1, opt2 = true }: { opt1?: boolean; opt2?: boolean; } = {}) {
console.log(opt2);
}
myFunction(); // outputs: true
It’s necessary in order to not destructure undefined:
function myFunction({ opt1, opt2 }) {
}
// Uncaught TypeError: Cannot destructure property `opt1` of 'undefined' or 'null'.
myFunction();