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';
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"
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"
How do I add with { type: "json" }
in import json from "./data.json" with { type: "json" };
?
@module({from: "./data.json", with: {type_: "json"}})
external json: JSON.t = "default"
Compiles to:
import DataJson from "./data.json" with {"type": "json"};
var json = DataJson;