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

`exportInterfaces:true` doesn't work with labeled arguments

tsnobip opened this issue · comments

if you have "exportInterfaces": true in your bsconfig.json and have this inside Foo.res:

@genType
type bar = {baz: int}

@genType
let process = (~bar) => bar.baz

This is generated in Foo.gen.ts:

/* TypeScript file generated from Foo.res by genType. */
/* eslint-disable import/first */


// @ts-ignore: Implicit any on import
const FooBS = require('./Foo.bs');

export interface Ibar { readonly baz: number };

export const process: (_1:{ readonly bar: bar }) => number = function (Arg1: any) {
  const result = FooBS.process(Arg1.bar);
  return result
};

As you can see, the type bar doesn't exist and should be Ibar.

Indeed, thanks for reporting.
The setting exportInterfaces is kind of experimental.
Would you explain your use case? Would you use it frequently or is this only an experiment?

Yeah I know it's kind of experimental and we started using it recently but I think it's very convenient for one use-case.

Let's say you have to deal with a very complex type from typescript but only use a subset of it from rescript, what we do is that we define this subset in rescript as an interface with genType then import it from typescript and extend this interface to add the fields we only use in typescript.

I could otherwise define the type entirely in rescript and import the types from typescript as opaque types for the fields I don't use in rescript, but it's a bit cumbersome and easily leads to circular dependencies.

Maybe it makes sense to activate it on specific values/files, and move it off being an experimental option.
First though I'd need to check that this does not require information to travel across files.

@tsnobip here's a fix:
#563

Let me know if it solves your general problem.

Thanks a lot @cristianoc.
Do you have any example of using a version of genType from a PR?
When using "gentype": "rescript-association/genType#pull/563/head" I get this error:

Fatal error: exception Failure("gentype/gentype.exe not found when resolving gentype.exe")

OK I have cloned it and built it and it indeed solves the issue, thanks @cristianoc :)