Skip to content

A Rust vector with enum elements should result in a TypeScript array of enum elements instead of any elements #4480

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

Open
johanpel opened this issue Apr 14, 2025 · 0 comments

Comments

@johanpel
Copy link

johanpel commented Apr 14, 2025

Motivation

From this Rust program:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub enum MyEnum {
    A,
    B,
}

#[wasm_bindgen]
pub fn foo() -> Vec<MyEnum> {
    unimplemented!()
}

#[wasm_bindgen]
pub fn bar(values: Vec<MyEnum>) -> MyEnum {
    unimplemented!()
}

the following TS code is generated:

export function foo(): any[];
export function bar(values: any[]): MyEnum;
export enum MyEnum {
  A = 0,
  B = 1,
}

Proposed Solution

The binding code is functional, but for the sake of improved type-safety on the TypeScript side it would be nice if:

  1. foo would return MyEnum[] instead of any[]
  2. bar would take MyEnum[] instead of any[] for the values argument.

Note that the return value of bar is indeed MyEnum already.

Alternatives

I can't think of any other sensible alternatives to what is proposed.

Additional Context

None.

@johanpel johanpel changed the title A Rust vector with enum elements should result in a TypeScript array of enum elements of any elements A Rust vector with enum elements should result in a TypeScript array of enum elements instead of any elements Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant