Why would it matter where a type declaration is located?

105 views Asked by At

The documentation for ReasonReact specifies where in the code a type should be declared:

The state type could be anything! An int, a string, a ref or the common record type, which you should declare right before the reducerComponent call:

type state = {counter: int, showPopUp: bool};

let component = ReasonReact.reducerComponent "Dialog";

The emphasis is theirs.

Why would it matter where the type declaration is located, so long as it's valid? Does some kind of optimization take place only when the two lines are adjacent? What happens if I insert other things between them or put them in separate modules?

2

There are 2 answers

0
glennsl On BEST ANSWER

The type needs to be defined before it is used, but it doesn't matter in any technical sense whether there's anything in between. That's just convention, to keep related things together.

I'll see about getting this clarified in the docs.

1
chenglou On

Putting the state type (or the retainedProps type or the action type) after the component definition will give you a type error; if you turn on super-errors (like so: https://github.com/reasonml-community/bs-glob/blob/d891ce1fadd3f3b2938d5900eb15241be4a3c1d0/bsconfig.json#L3) then the error briefly explains itself.

In short, it's a corner-case typing issue (scope escape), whose explanations you can find elsewhere.