Skip to content

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

Open
@johanpel

Description

@johanpel

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions