Format confusing WRT optional parameter annotations

Wen I’m writing out type interfaces I often will hover over a type, and copy the produced type annotation as the type definition. The type hover sticks a question mark between the tilde and the parameter name when dealing with optional named args, for example:


However, the formatter strips the question mark when the file is saved:

CleanShot 2021-04-15 at 09.24.45@2x

It took me a while to figure out that the correct annotation for an optional named argument is to throw an =? at the end of the parameter. I’m making a note here for anyone else who runs into this.

So, in summary, don’t do

let blah: (~?whatever: string, int) => int

Instead do:

let blah: (~whatever: string=?, int) => int

Thanks for that example =). Maybe the error message can be improved. We’ve got a type signatures syntax cheatsheet here.

For functions optional parameter this is the syntax
~second: int=?

But for a react component
~second: option<int>=?

Why should the optional prop be wrapped with option in a component?

@a-c-sreedhar-reddy the difference isn’t about react components. It’s about inside vs outside of a function. If you were to write that entire function with heavy annotations it might look like:

let blah:
  // you don’t need to pass `whatever` but if you do it’s a string
  (~whatever: string=?, int) => int
  // inside the function whatever is an option<string>
  = (~whatever: option<string>=?, i: int) =>
    switch whatever {
    | None => i
    | Some(_) => i +1