bytecodealliance/jco

plain enums are generating complex typescript

Closed this issue · 8 comments

enum Align {
  Left,
  Center,
  Right
}

generates

export type Align = AlignLeft | AlignCenter | AlignRight;
export interface AlignLeft {
  tag: 'left',
}
export interface AlignCenter {
  tag: 'center',
}
export interface AlignRight {
  tag: 'right',
}

isn't this too complex? Can it just be export type Align = 'left' | 'center' | 'right' ?

Maybe it could be, assuming the bindings were generated to match. Going from an { tag: string } to just a string.

maybe I can add a PR for this. Could you kindly point out the relevant code for me?

I'm not quite up to speed with this repo, yet. So I cannot give guidance. It might not be a simple change.

Can you share an example of how you are getting this output?

For the example WIT:

test.wit

package local:pkg;

interface align {
  enum align {
    left,
    center,
    right
  }

  p: func(a: align);
}

world p {
  export align;
}

Running jco types test.wit -o types gives an output of:

export namespace LocalPkgAlign {
  export function p(a: Align): void;
}
/**
 * # Variants
 * 
 * ## `"left"`
 * 
 * ## `"center"`
 * 
 * ## `"right"`
 */
export type Align = 'left' | 'center' | 'right';

@guybedford i'm using cargo component build --release and jco compile. Do you need a sample project or something?

anyway I setup a simple repo https://github.com/zimond/jco-enum-bug
cargo component build --release
jco transpile target/wasm32-wasi/release/jco-enum-bug.wasm -o pkg

@zimond you've used a variant instead of an enum. If you rename variant align to enum align you get the enum behaviour.

Ah I get it. Sorry for taking your time.