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

async functions don't have proper type signatures in Typescript #2394

Closed
thomaseizinger opened this issue Dec 8, 2020 · 7 comments · Fixed by #2665
Closed

async functions don't have proper type signatures in Typescript #2394

thomaseizinger opened this issue Dec 8, 2020 · 7 comments · Fixed by #2665
Labels

Comments

@thomaseizinger
Copy link
Contributor

Describe the Bug

As soon as a function is async, the return type in the Typescript definition is always any.

Steps to Reproduce

  1. Annotate an async function with #[wasm_bindgen]:
#[wasm_bindgen]
pub fn greet() -> String {
    "Hello World!".to_owned()
}

#[wasm_bindgen]
pub async fn async_greet() -> String {
    "Hello World!".to_owned()
}
  1. Run wasm-pack build

  2. Observe the generated typescript types:

/* tslint:disable */
/* eslint-disable */
/**
* @returns {string}
*/
export function greet(): string;
/**
* @returns {any}
*/
export function async_greet(): any;

Expected Behavior

The type definition for async_greet should be:

export function async_greet(): Promise<string>;

Actual Behavior

Function has return type any.

@alexcrichton
Copy link
Contributor

Thanks for the report! This is due to how async desugars internally in the wasm-bindgen macro, aand all promises are returned as any (unfortunately) rather than with their type parameters. This would be great to fix, however!

@tvolk131
Copy link

Also running into this problem - would love to see a fix! I'd be happy to work on it, but would need some direction since I'm unfamiliar with the codebase and somewhat new to writing macros.

@sdykae
Copy link

sdykae commented Jul 12, 2021

@alexcrichton why #[wasm_bindgen(typescript_type = "Promise<string>")] does not work?

@jeremyBanks
Copy link

@iuslkae Darn, I was just trying that now and landed here. :(

@sdykae
Copy link

sdykae commented Jul 18, 2021

@jeremyBanks maybe there is a way to override the output at own risk, cuz the tool they are using is working worng. Could be good if they document how to achieve this to make a pull req. Since the I'm just logging the responses to get their tipes and type each one on projects i work ;'D

@arnauorriols
Copy link
Contributor

This not only affects Typescript types. JSDoc autogenerated documentation also outputs any instead of the proper type.

@trevyn
Copy link
Contributor

trevyn commented Aug 25, 2021

@arnauorriols My PR linked above (which looks on track to be merged soon) also adds the JSDoc comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants