Skip to content

Include corresponding values in d.ts for C-style enums #2730

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

Closed
remagpie opened this issue Dec 3, 2021 · 1 comment · Fixed by #3239
Closed

Include corresponding values in d.ts for C-style enums #2730

remagpie opened this issue Dec 3, 2021 · 1 comment · Fixed by #3239

Comments

@remagpie
Copy link

remagpie commented Dec 3, 2021

Motivation

wasm_bindgen macro supports C-style enums. It supports specifying literal values for each enum variant, but it seems that those values are not included in the generated d.ts file. For example,

#[wasm_bindgen]
pub enum ErrorCode {
    FooError = 0,
    BarError = 2,
}

is converted to the following code:

export enum ErrorCode {
  FooError,
  BarError,
}

Specifying the actual values of enum lets us use more features in typescript, so it would be nice if the corresponding literal values are included in d.ts.

Proposed Solution

The ideal solution would give us the following typescript code:

export enum ErrorCode {
  FooError = 0,
  BarError = 2,
}
@Dollab
Copy link

Dollab commented Jan 12, 2023

👍 This is really important as otherwise it can lead to subtle bugs when using the literal value of the enum.

In particular, non initialized enum in the typescript declaration file generated by wasm_bindgen will be computed. This all prevents the use of the enum variants in type declarations.

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

Successfully merging a pull request may close this issue.

2 participants