Using open but for just one type?

One of my favorite things about rescript is it’s ability to infer a type from usage. But that only works when the type is in the same module, I was wondering if there’s some kind of way to “open” that type into a another module but without the baggage of importing the rest of the module?

open User.t

Unfortunately, you can only do that with values, not types:

let {make} = module(User) (works)
type {t} = module(User) (does not work)

but I found out you can kinda do it with record spreads if the user type is a record.

type t = {...User.t}

Beware that this will create a new type that’s considered another type than User.t. You can of course use coercion with that type (someTValue :> User.t) but still, not quite the same as using the original type.

1 Like

Right, it’s not really the equivalent of open but of include.

Another way to do it is to have type t in a submodule and only open that one.

module User = {
  module Type = { 
    type t = { ... }
  }
  ...
}

open User.Type.t
1 Like

Yeah having it in a submodule is probably the least invasive way of doing it.

Namespacing your types inside a module is a good habit anyway!

Isnt there a substitution type assignment?
type t := User.t

Generally strikes me as a bad idea but maybe?