What is the difference between option<t>=? and just =? in ReScript JSX?

25 views Asked by At

From the documentation of ReScript React, I can declare a component like this:


module OptionalChildren = {
  @react.component
  let make = (~children: option<React.element>=?, ~children2: React.element=?) => {
    <div>
      {switch children {
      | Some(element) => element
      | None => React.string("No children provided")
      }}
      {switch children2 {
      | Some(element) => element
      | None => React.string("No children provided")
      }}
    </div>
  }
}

Both children and children2 evaluate to option<React.element> type? So, what's the difference? Is latter just the short form of former?

1

There are 1 answers

0
glennsl On BEST ANSWER

The first form is explicit, since children will actually be an option<_>. The second form will emit the warning:

React: optional argument annotations must have explicit option. Did you mean option(React.element)=??

The word "must" is a bit odd, since clearly it does compile without it. But I believe the motivation for it is type annotation accuracy, to avoid confusion. For example, what if your intention is actually option<option<React.element>>? How would you interpret seeing just option<React.element>?

Note also that it's possible to provide a default argument, ~children: React.element=React.null for example, which adds to the complexity of the construct.