How to bind to typescript mapped type?

Hi,

Is it possible to properly define a ReScript type to bind to a TypeScript object using mapped types like:

type t = {
  [key: string]: {
    children: string;
  };
}

Thanks :slight_smile:

When the keys aren’t known statically you’d use a Dict in ReScript:

type entry = {children: string}

type t = Dict.t<entry>

let dict: t = Dict.make()

dict->Dict.set("someKey", {children: "child"})

Compiles to:

var dict = {};

dict["someKey"] = {
  children: "child"
};

Playground: https://rescript-lang.org/try?version=v11.0.0-beta.4&code=PYBwpgdgBASmDOBjATgSxAFwMLGWAUPhgJ7hSQbLFQC8UA3ogBaoA2AJnhAFxTyWoIAcwC+hEmQy0oAEVSIMAOgwAeClQB8hVmCnt5GXlLpyFigLYBDANZgAFAEpC+hQFoNppfF12ARPGBzMABpMGJfABoGZjZOSF5fGI5fEScgA

3 Likes