Skip to content

Support for named and optional parameters in .d.ts #1129

Closed
@dtysky

Description

@dtysky

This issue is related to #982 which is fixed, but that only makes the return type will be reflected, if we use optional parameters, it still does not work. Another issue is that definitions of method will not keep their original name and use arg0... to instead, it may lose the semantic of parameters.

#[wasm_bindgen]
pub struct Color(f64, f64, f64, f64);

#[wasm_bindgen]
impl Color {
  #[wasm_bindgen(constructor)]
  pub fn new(r: Option<f64>, g: Option<f64>, b: Option<f64>, a: Option<f64>) -> Color {
    let mut color = Color(0.0, 0.0, 0.0, 0.0);

    match r {
      Some(value) => color.0 = value,
      None => ()
    }

    match g {
      Some(value) => color.1 = value,
      None => ()
    }

    match b {
      Some(value) => color.2 = value,
      None => ()
    }

    match a {
      Some(value) => color.3 = value,
      None => ()
    }

    color
  }
}

will generate dts:

export class Color {
  free(): void;
  constructor(arg0: number, arg1: number, arg2: number, arg3: number);
}

which I expect:

export class Color {
  free(): void;
  // use optional and keep semantic
  constructor(r?: number, g?: number, b?: number, a?: number);
}

Could we fix those issues for better programming experience?
Thanks.

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