Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Variant incorrect generated type #585

Closed
@tom-sherman

Description

@tom-sherman

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 vs tag
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions