genType icon indicating copy to clipboard operation
genType copied to clipboard

Generating Types From Third Party Imports Doesn't Work Automatically

Open chrischen opened this issue 5 years ago • 8 comments

I'm using genType to generate typescript code instead of plain javascript. In the generated TS files it references the Relude libs like so:

import {Bifunctor_t as Relude_Result_Bifunctor_t} from 'relude/src/Relude_Result.gen';

In the generated bs.js file it references import * as Relude_Result from "../../../node_modules/relude/src/Relude_Result.bs.js";

The Relude_Result.gen.tsx file doesn't actually exist. Is there a way to get this to work?

chrischen avatar Aug 31 '20 02:08 chrischen

The expectation is that annotations were added to relude directly, so the file would then be found.

cristianoc avatar Sep 01 '20 07:09 cristianoc

So the expectation is for libraries to add genType annotations? Is there a way for library consumers to do it?

chrischen avatar Sep 01 '20 08:09 chrischen

There's no way currently to annotate libraries from a client. One workaround one can try is to re-define the type as in

[@genType]
type qq = B.t = | A | B;

where the type from B is re-defined and exported locally.

cristianoc avatar Sep 01 '20 08:09 cristianoc

From #460, I think the issue name should be "genType don't annotate dependencies" or similar.


I wanted to share one bug that I have been facing using genType and generating TypeScript. Using BuckleScript 7.x and genType 3.18.0 (accordingly).

If I flag a variable with genType that has inferred a type from a dependency (in this case bs-js-collections), the output expects this library to have a gen file. I'm not sure if genType should generate those files (in my node_modules) for me or expect the lib to use genType as well.

I read about it here: https://github.com/reason-association/genType#dependent-projectslibraries but wasn't exactly sure about its behaviour with conjunction with "exportInterfaces": true.

The same happens with Js.Re, that expects to expose a gen file.

[@genType]
let set = JsSet.fromArray([|1, 2, 3|]);

[@genType]
let regex = [%re {|/[a-zA-Z_][a-zA-Z0-9_]*/|}];
/* TypeScript file generated from Index.re by genType. */
const IndexBS = require('./Index.bs');

import {Re_t as Js_Re_t} from './Js.gen';
import {t as JsSet_t} from 'bs-js-collections/src/JsSet.gen';

export const set: JsSet_t<number> = IndexBS.set;
export const regex: Js_Re_t = IndexBS.regex;
debug info
  Project root: /Users/davesnx/dev/playgrounds/try-genType-with-TS
  Config language:typescript module: importPath: shims:0 entries bsVersion:7.3.2
  Add Index.cmt  /Users/davesnx/dev/playgrounds/try-genType-with-TS/Index.re
  Translate Structure
  Translate Value Binding set
  fromPath path:JsSet.t typeEnv:__root__ External resolved:JsSet_t
  Dependency: JsSet_t
  Resolve Reason Module: JsSet
  Resolve Generated Module: JsSet
  Import Path: bs-js-collections/src/JsSet.gen
  Translate Value Binding regex
  fromPath path:Js.Re.t typeEnv:__root__ External resolved:Js_Re_t
  Dependency: Js_Re_t
  Resolve Reason Module: Js
  Resolve Generated Module: Js
  Import Path: ./Js.gen
  Create Type Map for Index
  Code Item: ExportValue resolvedName:set type:JsSet_t<number>
  Converter type0:JsSet_t<number> converter:id
  Code Item: ExportValue resolvedName:regex type:Js_Re_t
  Converter type0:Js_Re_t converter:id
  Replace  /Users/davesnx/dev/playgrounds/try-genType-with-TS/Index.gen.tsx

I created a playground to show-case this bug: https://github.com/davesnx/try-gentype-with-ts

Thanks

davesnx avatar Sep 09 '20 07:09 davesnx

I believe the third party library must have genType annotations and include the gen.ts files according to my reading of the docs and what cristianoc said above.

That being said, is this something that is a goal of the project, or should we be treating our genType exported types more explicitly in our code?

chrischen avatar Nov 12 '20 08:11 chrischen

I'm having a similar but worse problem - referencing a library module doesn't attempt to import library types from the library at all. I'm using rescript-fast-check and referencing (among other things) the arbitrary type, but the generated code is:

import type {arbitrary as Arbitrary_arbitrary} from './Arbitrary.gen';

I don't mind adding gentype annotations to the library but doing so makes no difference - this is trying to import it from the current folder.

[edit] that was with version 4.1.0, downgrading to 3.50.0 has the same problem but the names are slightly different. import type {Arbitrary_arbitrary as FastCheck_Arbitrary_arbitrary} from './FastCheck.gen';

TheSpyder avatar Jun 16 '21 03:06 TheSpyder

I'm having a similar but worse problem - referencing a library module doesn't attempt to import library types from the library at all. I'm using rescript-fast-check and referencing (among other things) the arbitrary type, but the generated code is:

import type {arbitrary as Arbitrary_arbitrary} from './Arbitrary.gen';

I don't mind adding gentype annotations to the library but doing so makes no difference - this is trying to import it from the current folder.

[edit] that was with version 4.1.0, downgrading to 3.50.0 has the same problem but the names are slightly different. import type {Arbitrary_arbitrary as FastCheck_Arbitrary_arbitrary} from './FastCheck.gen';

Having the exact same issue here, did you find any workaround?

searleser97 avatar Aug 26 '21 14:08 searleser97

With TypeScript mode from this

@genType
type arb = FastCheck.Arbitrary.arbitrary<int>

this is what I get:

import type {Arbitrary_arbitrary as FastCheck_Arbitrary_arbitrary} from 'rescript-fast-check/FastCheck.gen';

which is not far off, except for the fact that rescript-fast-check uses a namespace which here is not recognised.

cristianoc avatar Aug 28 '21 06:08 cristianoc

This repository is being archived. If you feel like the issue still relevant, please re-create it in the compiler repo. Thanks!

ryyppy avatar Nov 13 '23 13:11 ryyppy