How do I import Json?

I’m porting various typescript project as a way to learn rescript for great good.

How do I convert the following typescript import to rescript?

import schema from './schema.json';

Hi. Is this what you are looking for?

type schema = {
  name: string,
  age: int,
}
@module external leftPad: schema = "./schema.json"

PlayGround link.

5 Likes

This gives a type to the data what’s in the Json.
I guess I kind of didn’t expect it also needs a type.

This is for a test double I’m writing, but in the real app, the schema will be determined at runtime, so I can’t do:

type schema = {
  name: string,
  age: int,
}

To dynamically decode JSON data at runtime, you can use the Js.Json module. Your external code would look like this: @module external schema: Js.Json.t = "./schema.json"

1 Like