Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional object parameter in typescript

Tags:

typescript

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.

like image 545
LazioTibijczyk Avatar asked Jun 18 '26 12:06

LazioTibijczyk


1 Answers

Maybe this is what you are trying to achieve.

function renderComponent(openDialog? : { openDialog?: boolean }) {
    render(
        <Component />
    );

    if (openDialog) clickButton('Import Existing');
}
like image 105
Mario Sarbinov Avatar answered Jun 21 '26 14:06

Mario Sarbinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!