rescript-association / genType

Auto generation of idiomatic bindings between Reason and JavaScript: either vanilla or typed with TypeScript/FlowType.

Home Page:https://rescript-lang.org/docs/gentype/latest/introduction

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong type generated for variant

erikras opened this issue · comments

TLDR;

My event variant type is treated as an integer in JS, and a string in TS.

My ReScript

export type event = Increment | Decrement

let inc = Increment

Generated JavaScript

var inc = /* Increment */ 0;

export { inc }

Generated TypeScript

export type event = "Increment" | "Decrement";

Versions

{
  "gentype": "^3.48.0",
  "bs-platform": "9.0.2",
}

inc is not exported, add a @genType to it (or export, but that notation is being phased out) and the conversion will take place.

That's not the problem. The generated event type is still string in TypeScript.

It would be nice if you would try running it yourself. It's just two lines.

This has the same problem:

@genType
type event = Increment | Decrement

@genType
let inc = Increment
/* TypeScript file generated from A.res by genType. */
/* eslint-disable import/first */


const $$toJS914395812: { [key: string]: any } = {"0": "Increment", "1": "Decrement"};

// @ts-ignore: Implicit any on import
import * as ABS__Es6Import from './A.bs';
const ABS: any = ABS__Es6Import;

// tslint:disable-next-line:interface-over-type-literal
export type event = "Increment" | "Decrement";

export const inc: event = $$toJS914395812[ABS.inc];

This ^ is the conversion. You use it as string from TS, and keeps the internal representation on the ReScript side.

You might want to use #This | #That to have the same runtime representation, and zero conversion, on the TS and ReScript sides.

Here is a reproduction of my problem.
https://github.com/erikras/rescript-gentype-bug

@erikras can you add @genType to the type definition

@genType
type machine<'state, 'event> = {

It should solve your issue.

That said, ideally there should be a detection for this. This being: a type not exported is used indirectly from a function exported in another module, so it's implicitly exported.

That did it! Thank you!

Very counter-intuitive that to fix type generation in counter.res, I need a @genType in Machine.res.

Very counter-intuitive that to fix type generation in counter.res, I need a @genType in Machine.res.

Indeed.