Create a Js.t based type

There is an object named “req” in JavaScript side, it has many methods and properties, I want to give it a Js.t object type.

I can use {…} as a object type in the argument, for example
let f = (req :{…}) => somecode
But I didn’t know how to use {…} in the type variable.
type req = {…}
type req = Js.t<{…}>
The code above will generate an error: a type variable is unbound.
type req = {.} it works, but only for empty object.

Is it a limit for rescript? I remember I can use Js.t({…}} in reasonml.

1 Like

Hi, Good news is in next release, Js.t is not relevant, it is still there (for compatibility) but it is an identity type:

type t <'a> = 'a

So in that case, your confusion will disappear automatically

3 Likes

You don’t need to worry about the Js.t detail in ReScript.

Check out the object section in the language manual.

For example, your req could look like this:

type req = {
  "headers": Js.Dict.t<string>,
 "data": Js.Json.t
}

let r = {
  "headers": Js.Dict.empty(),
 "data": Js.Json.parseExn(` "hello" `)
}

Js.t is the hidden representation of a JS object type, hence you need to use the JS object type syntax like stated above. The Js.t type detail will go away as Bob stated.

2 Likes