Please see this minimum function
function addX({ num = 1 }) {
return num + 1;
}
The above function has done two things
- Deconstruct function parameter
- Assign default value
Now, I can use the function like this:
add({ num: 2 }); // valid
add({}); // valid
However, I can't use it like this
add(); // Expected 1 arguments, but got 0
I can't find a way to tell TypeScript the whole object is optional
// Invalid
function add({ num = 1 }?) {
return num + 1;
}
// Invalid
function add({ num = 1 }?: { num: number }) {
return num + 1;
}
How can I achieve it?