How to make genType work nice with built-in/standard types?

I am trying to explore as well as learn the possibility of integrating ReScript into an existing Node/TypeScript backend project. Since, the project closely follows Hexagonal architecture, I feel ReScript is a nice fit there considering the types are defined in the domain layer and persistence and API layer simply implement those.

I started with a very simple type.

@genType
type node = {
  id: string,
  name: string,
  description: string,
  key: int,
  createdAt: Js.Date.t,

  // PROBLEM:
  updatedAt: Date.t,
  elm: Dom.element,
  parent: Dom.document,
}

This produced following TypeScript code:

/* TypeScript file generated from ContextType.res by genType. */
/* eslint-disable */

import type { document as Dom_document } from './Dom.gen.js';
import type { element as Dom_element } from './Dom.gen.js';
import type { t as Date_t } from './Date.gen.js';

export type node = {
  readonly id: string; 
  readonly name: string; 
  readonly description: string; 
  readonly key: number; 
  readonly createdAt: Date; 
  readonly updatedAt: Date_t;
  readonly elm: Dom_element; 
  readonly parent: Dom_document
};

The good thing here is that Js.Date.t is cleanly mapped to native JS/TS Date type whereas other types Date.t Dom.element Dom.document try to map to some non-existent types. Additionally, it seems to be importing these types from some non existent relative files ./Date.gen.js, ./Dom.gen.js;

Is there a way around this problem? And, are there any more pitfalls that I should be aware of before attempting to migrate the core business logic from TS to ReScript?

Yep!

You are looking for genType shims: Referencing standard types in TS genType - #2 by illusionalsagacity

Thanks for the reply. I guess I can try this over the weekend. But, isn’t the TS shim a deprecated feature as of ReScript 11: TypeScript | ReScript Language Manual

v12 aims to remove TS Shims. However, since no official alternative currently exists, users will still have to rely on shims when necessary.

Another alternative is adding genType output to core packages, such as Js and @rescript/core. This method may temporarily inflate the contents of the package and may add excessive migration burden in the future.

Since genType output is now a “runtime” dependency, there is some actual overhead, which is bad for users. It is also bad for maintainers because it requires an additional tsc build it.

Shims solve some use cases, so I may add documentation for that again.

3 Likes

I think publishing genType output might be a good idea. I’ve done it for rescript-schema to make the users life easier https://github.com/DZakh/rescript-schema/blob/main/RescriptSchema.gen.ts.

There are two things to note:

  1. GenType is not really supported with custom namespaces, so I had to write the file manually (I’m not sure whether it’s already changed)
  2. Since only the types are used, we can keep only them, omitting all the values from the .gen.ts