-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Improved TS type generation from WASM #4229
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some integration tests for those changes?
There currently isn't any infrastructure for testing this, so how would you like me to test this? This might be a hammer and nail situation for me, since I've done a lot with What do you think? |
Yes, that would be great! |
Alright, I added a test with I initially thought about adding a feature for type-only tests, but then removed it again, because I wanted to see the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Multi-value returns are now typed with tuple types. E.g.
(a: number) => [number, bigint, number]
. This accurately represents the actual result of WASM functions.
We are currently only testing [number, number]
output. How about testing u128
and Option<u128>
?
I should probably mention that, right now, this almost entirely affects code that we shouldn't generate in the first place. Just guessing the historical context here, but I assume this was actually implemented in the first place to generate a TS interface for functions not exported via wasm-bindgen
, but via extern "C" fn() { ... }
. Unfortunately this inadvertently also exported all the internal wasm-bindgen
functions, which should never be used. Ideally, we identify all these functions and don't export them as part of InitOutput
. But maybe I'm missing something and these exports have a use-case after all.
However, the general code improvements are obviously very appreciated!
Done.
I wonder whether this is how people actually use it. Given that proposals like So if users really are not supposed to even touch the functions exported with |
We should indeed, but we should definitely not expose them in the generated TS as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent!
This PR improves the TS generation for the WASM file and the
InitOutput
interface.Changes:
module_export_types
function.module_export_types
uses interior iteration to pass all name-type pairs to the respective function for formatting.number
.(a: number) => [number, bigint, number]
. This accurately represents the actual result of WASM functions.Another mostly aesthetic change is that the generated
.d.ts
files for the.wasm
file don't useexport function name(arg: S): T
anymore. They now usesexport const name: (arg: S) => T
. Typing-wise, these two styles are equivalent, but the arrow function style makes code generation easier, because it allows the code to be shared between the.d.ts
and interface code.Before:
After:
Just like #4210, this PR also fixes #4207. However, the goal of this PR was not to fix this issue. So please merge #4210 before this PR if possible.