Skip to content
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

Allow to manually specify typescript function signatures #2155

Closed
Vlad-Shcherbina opened this issue May 25, 2020 · 1 comment
Closed

Allow to manually specify typescript function signatures #2155

Vlad-Shcherbina opened this issue May 25, 2020 · 1 comment

Comments

@Vlad-Shcherbina
Copy link
Contributor

Motivation

TypeScript has a very expressive type system.
Some of its features have no counterparts in the Rust type system, and in any case wasm-bindgen is unlikely to ever support it in full.
This matters for making high-quality TypeScript APIs for wasm modules, so maybe there should be an escape hatch to manually specify more subtle types than wasm-bindgen could infer.

Example:

// .rs
#[wasm_bindgen]
pub fn find_root(f: &js_sys::Function) -> f64 { ... }
// .d.ts
export function find_root(f: Function): number;  // inferred by wasm-bindgen, bad
export function find_root(f: (x: number) => number): number;  // good

Proposed Solution

Support the following syntax on exported Rust functions:

#[wasm_bindgen(typescript_signature="(x: number): -1 | 0 | 1")]
pub fn sign(x: i32) -> i32 { ... }

When typescript_signature is specified, wasm-bindgen should uncritically use it in the generated .d.ts:

export function sign(x: number): -1 | 0 | 1;
// instead of the default 'export function sign(x: number): number;'

The responsibility to ensure that it matches the actual implementation is on the programmer.

Additional Context

This feature could also serve as a workaround for #1591 and #1197 .

@Pauan
Copy link
Contributor

Pauan commented May 25, 2020

This is already possible by using typescript_type, like this:

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(typescript_type = "-1 | 0 | 1")]
    type Order;
}

And now your functions can accept/return Order:

use wasm_bindgen::JsCast;

#[wasm_bindgen]
pub fn sign(x: f64) -> Order {
    JsValue::from(x).unchecked_into::<Order>()
}

Note the usage of unchecked_into to cast it into the Order type.

This also works for putting Order into structs:

#[wasm_bindgen]
pub struct Foo {
    pub order: Order,
}

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

2 participants