Issue
I am creating a simple function with an object parameter. It accepts openDialog
boolean property which decides whether to run another function. I am not defining an interface for it because it is ever going to be a single property. My issue is how do I define it in such a way that I am no longer required to pass an empty object to that function? openDialog
is optional so what is the right syntax for making that object optional too?
function renderComponent({ openDialog }: { openDialog?: boolean }) {
render(
<Component />
);
if (openDialog) clickButton('Import Existing');
}
Typescript complains about renderComponent()
with
Expected 1 arguments, but got 0.
It accepts renderComponent({})
I am aware of a default parameter function renderComponent({ openDialog }: { openDialog?: boolean } = {}) {
but I am wondering whether there is a different way.
Solution
Maybe this is what you are trying to achieve.
function renderComponent(openDialog? : { openDialog?: boolean }) {
render(
<Component />
);
if (openDialog) clickButton('Import Existing');
}
Answered By - Mario Sarbinov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.