Referencing standard types in TS genType

When using genType for creating typescript types, whenever I reference a (supposedly) standard api, it tries to pull the type from some imaginary module

A simple example is using Promise.t or just Js.Dict.t

@genType
type state = {
  promise: Promise.t<unit>,
  dict: Js.Dict.t<string>,
}

It generates this TS file with imports to these modules, neither which exist:

import type {Dict_t as Js_Dict_t} from './Js.gen';
import type {t as Promise_t} from '@ryyppy/rescript-promise/src/Promise.gen';

// tslint:disable-next-line:interface-over-type-literal
export type state = { readonly promise: Promise_t<void>; readonly dict: Js_Dict_t<string> };

This leads to compilation errors when trying to bundle my library. Is there a way to coerce it to use TS’s built in Promise<void> or Record<string, string> instead?

1 Like

Yes, via genType shims. I believe there was some finickiness when I was setting up shims for my work’s repo.

It’d be a pretty nice feature to be able to supply the shim inside a given rescript library to avoid this.

You can also use Js.Promise.t instead to avoid needing to write a shim. Promise.t I believe is just an alias but gentype doesn’t seem to deeply resolve those unfortunately.

2 Likes

The same problem with me!

Does anyone know how how the shims actually work, and what the config is meant to be?

I’m trying to create shims for the new RescriptCore bindings but they just refuse to be resolved.

The type I’m using:

type t = Dict.t<string>

My bsconfig.json:

"shims": {
  "Dict_t": "Dict",
  "Dict": "Dict",
  "RescriptCore.Dict": "Dict",
  "RescriptCore": "RescriptCore"
}

src/Dict.shim.ts

export type t<a> = Record<string, a>;
export type Dict_T<t> = Record<string, t>;

How the TS is outputting:

// ts output
import type {t as Dict_t} from './Dict.gen';

I tried shims for the first time now.

Can you try adding Js to the shims

"shims" : {
   "Js": "Js"
}

and create a shim file

// src/shims/Js.shim.ts
export type Dict_t<T> = {[id: string]: T};

Here is an example repo

1 Like

I think instead of using shims rescript-core should publish .gen.ts with types

2 Likes

This works for the Js. module, but not when using @rescript/core from [ANN] ReScript Core - a new, batteries included drop-in standard library - Announcements - ReScript Forum (rescript-lang.org).

I think it might be have something to do with the bsconfig -open flag. I did the same thing as the Js. with RescriptCore. and see the Js one resolves but core doesn’t

@genType
type js_dict = Js.Dict.t<string>

@genType
type dict = Dict.t<string>
/* TypeScript file generated from Demo.res by genType. */
/* eslint-disable import/first */


import type {Dict_t as Js_Dict_t} from '../src/shims/Js.shim';

import type {t as Dict_t} from './Dict.gen';

// tslint:disable-next-line:interface-over-type-literal
export type js_dict = Js_Dict_t<string>;

// tslint:disable-next-line:interface-over-type-literal
export type dict = Dict_t<string>;

I’ve set up my issue in a repo in case I’m unclear

I’m having same problem. Can’t figure out shims.

See this PR, and the Dict.t treatment here: Add `Core` standard library support for `genType` by cristianoc · Pull Request #6019 · rescript-lang/rescript-compiler · GitHub

Comment there directly for anything missing.

2 Likes