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

[genType "xxx"] should not trigger a warning/error.

davesnx opened this issue · comments

Hey @cristianoc,

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 "2set"] generates invalid JS/TS. Would be a good warning/error to show to users allowing them only write valid TS. With a fast check on StackOverflow I found a good explanation (I can't find it on ECMA specs (https://www.ecma-international.org/ecma-262/#prod-VariableStatement) 😢 ).

[@genType "2set"]
let set = JsSet.fromArray([|1, 2, 3|]);
/* TypeScript file generated from Index.re by genType. */
const IndexBS = require('./Index.bs');

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

export const 2set: JsSet_t<number> = IndexBS.set2;
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 set2
  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
  Create Type Map for Index
  Code Item: ExportValue resolvedName:2set type:JsSet_t<number>
  Converter type0:JsSet_t<number> 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 since genType runs as a post-processing step, it can't trigger an error that stops the build at the moment.
It could avoid generating invalid TS but then it would be harder to understand what went wrong.

Cool.

I'm not familiar with any post-process after BS but, could we print some warnings into the same stdout as BuckleScript?

The idea isn't to avoid generate invalid TS, for me it was to understand what genType does and is capable of doing. When I add a genType notation and nothing pops up to the TS file I have no guide on where the error/mistake is made.

a random question, is there any plan to change the genType to rely on post-hooks in the future of rescript?

Thanks for the response.

Btw do you get an error from TypeScript?
The idea is when there's a type mismatch, the type error will appear in TS. And this case is not a type mismatch, just an invalid identifier, but the mechanism could be the same.

Moving away from post-hook could happen in future, it would be nice. But not in the near future.

Yes you got the compiler errors, generating TS gives the compiler errors and having untyped output it might not be the case, but if you are running ESLint in one way or another, it will pop as error as well.