Skip to content

Support for Option in .d.ts #982

Closed
Closed
@henriiik

Description

@henriiik

Currently, if you return an Option from a function in rust, the generated .d.ts file does not reflect that the function can return undefined. I am willing to help out if this is a desired change. 🙂

For example if you have the following rust,

extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn some_number() -> usize {
    1
}

#[wasm_bindgen]
pub fn some_optional_number() -> Option<usize> {
    None
}

you get the following d.ts,

/* tslint:disable */
export function some_number(): number;

export function some_optional_number(): number;

But the actual return value has the type number | undefined. if you run typescript in strict mode, it will force you to check for undefined, just like rust.

Or you could go even further and define a new type in typescript, ex:

type Option<T> = T | undefined;

export function some_optional_number(): Option<number>;

Would you be willing to merge a change like this if I send a PR? 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions