Variant incorrect generated type
tom-sherman opened this issue · 5 comments
First raised on the forum here: https://forum.rescript-lang.org/t/gentype-different-representation-in-rescript-compared-to-generated-typescript/2750
Repro:
Given the following module https://github.com/tom-sherman/rescript-xstate/blob/0e97ec7ef321ceb6408ad52cf7b245f7cfdc6f13/src/XStateFunctor.res
@genType
type rec stateNode<'state, 'event> =
| State({name: 'state, on: array<('event, 'state)>})
| Compound({name: 'state, children: array<stateNode<'state, 'event>>, initial: 'state})
| Final({name: 'state})
@genType
type stateList<'state, 'event> = array<stateNode<'state, 'event>>
It outputs this typescript
export type stateNode<state,event> =
{ tag: "State"; value: { readonly name: state; readonly on: Array<[event, state]> } }
| { tag: "Compound"; value: {
readonly name: state;
readonly children: stateNode<state,event>[];
readonly initial: state
} }
| { tag: "Final"; value: { readonly name: state } };
// tslint:disable-next-line:interface-over-type-literal
export type stateList<state,event> = stateNode<state,event>[];
However this type is incorrect, as the values of the stateNode
variant is something like:
{ TAG: 0, name: 'idle', on: [ [ 'FETCH', 'loading' ] ] }
Differences:
TAG
vstag
- Tag values are integers vs the variant constructor names
- Variant payload is nested in a
value
field vs existing as properties at the top level of the object
Hi,
Yeah the representation of variants in the generated javascript and the typescript are not same. But this would not break the app and is type safe.
So genType generated bindings are in such a way that it accepts the typescript type representation
and then converts them to ReScript representation
before passing to rescript functions.
For the following ReScript code
@genType
type rec stateNode<'state, 'event> =
| State({name: 'state, on: array<('event, 'state)>})
| Compound({name: 'state, children: array<stateNode<'state, 'event>>, initial: 'state})
| Final({name: 'state})
@genType
let logStateNode = (a: stateNode<string, string>) => Js.log(a)
The generated typescript bindings are
/* TypeScript file generated from Test2.res by genType. */
/* eslint-disable import/first */
// @ts-ignore: Implicit any on import
import * as Test2BS__Es6Import from './Test2.bs';
const Test2BS: any = Test2BS__Es6Import;
// tslint:disable-next-line:interface-over-type-literal
export type stateNode<state,event> =
{ tag: "State"; value: { readonly name: state; readonly on: Array<[event, state]> } }
| { tag: "Compound"; value: {
readonly name: state;
readonly children: stateNode<state,event>[];
readonly initial: state
} }
| { tag: "Final"; value: { readonly name: state } };
export const logStateNode: (a:stateNode<string,string>) => void = function (Arg1: any) {
const result =
/* WARNING: circular type stateNode. Only shallow converter applied. */
Test2BS.logStateNode(Arg1.tag==="State"
? Object.assign({TAG: 0}, Arg1.value)
: Arg1.tag==="Compound"
? Object.assign({TAG: 1}, Arg1.value)
: Object.assign({TAG: 2}, Arg1.value));
return result
};
Note There's a warning:
/* WARNING: circular type stateNode. Only shallow converter applied. */
The conversion is not supported for recursive types. Because of complexity and unclear semantics. It would at best make a deep copy, lose sharing, etc.
I think I've the same problem. I use ts-pattern in order to handle output of a rescript function from typescript (but exact same thing with a switch
statement)
In my case it's based on a Belt.Result.t<'a, 'e>
.
the generated typescript gives me a
type MyResult<a,e> =
{ tag: "Ok"; value: a }
| { tag: "Error"; value: e };
so I can pattern match on it (typescript side) with
match(result)
.with({ tag: "Ok" }, () => ...)
.with({ tag: "Error"}, () => ...)
but it breaks at runtime because it receives something like {"TAG":1,"_0":0}
do you know how I can overcome this (without handling the result in rescript)
EDIT : here's a minimal example : https://github.com/err0r500/rescript-ts-issue-minimal
Hi,
Yeah the representation of variants in the generated javascript and the typescript are not same. But this would not break the app and is type safe.
So genType generated bindings are in such a way that it
accepts the typescript type representation
and thenconverts them to ReScript representation
before passing to rescript functions.For the following ReScript code
@genType type rec stateNode<'state, 'event> = | State({name: 'state, on: array<('event, 'state)>}) | Compound({name: 'state, children: array<stateNode<'state, 'event>>, initial: 'state}) | Final({name: 'state}) @genType let logStateNode = (a: stateNode<string, string>) => Js.log(a)The generated typescript bindings are
/* TypeScript file generated from Test2.res by genType. */ /* eslint-disable import/first */ // @ts-ignore: Implicit any on import import * as Test2BS__Es6Import from './Test2.bs'; const Test2BS: any = Test2BS__Es6Import; // tslint:disable-next-line:interface-over-type-literal export type stateNode<state,event> = { tag: "State"; value: { readonly name: state; readonly on: Array<[event, state]> } } | { tag: "Compound"; value: { readonly name: state; readonly children: stateNode<state,event>[]; readonly initial: state } } | { tag: "Final"; value: { readonly name: state } }; export const logStateNode: (a:stateNode<string,string>) => void = function (Arg1: any) { const result = /* WARNING: circular type stateNode. Only shallow converter applied. */ Test2BS.logStateNode(Arg1.tag==="State" ? Object.assign({TAG: 0}, Arg1.value) : Arg1.tag==="Compound" ? Object.assign({TAG: 1}, Arg1.value) : Object.assign({TAG: 2}, Arg1.value)); return result };
This seems to not work when dealing with interfaces.
Fixed in compiler v11