Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imperfect enum of TypeScript. #2045

Open
s-shin opened this issue Mar 18, 2020 · 0 comments
Open

Imperfect enum of TypeScript. #2045

s-shin opened this issue Mar 18, 2020 · 0 comments
Labels

Comments

@s-shin
Copy link

s-shin commented Mar 18, 2020

Describe the Bug

enum definition is generated in d.ts from enum type of rust, but the actual code in JS is not compatible with TS. This can cause unexpected errors for example in reverse mappings.

Steps to Reproduce

#[wasm_bindgen]
pub enum Foo {
    A,
    B,
    C,
}

This code will be converted to JS:

export const Foo = Object.freeze({ A:0,B:1,C:2, });

and TS (in d.ts):

export enum Foo {
  A,
  B,
  C,
}

but these are not completely compatible.

Expected Behavior

Consistency should be kept.

Actual Behavior

We can make JS code compatible with TS like this:

export var Foo;
(function (Foo) {
    Foo[Foo["A"] = 0] = "A";
    Foo[Foo["B"] = 1] = "B";
    Foo[Foo["C"] = 2] = "C";
})(Foo || (Foo = {}));

or can also do TS definition compatible with JS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant