Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@guybedford
Copy link
Contributor

@guybedford guybedford commented Oct 10, 2025

This extends enum support to work with singular untagged enums as a form of union type, allowing a representation for TypeScript union types using runtime generics in wasm bindgen.

Per standard generics, we treat JsValue as the target, then apply runtime checks to convert from JS into the appropriate type using the TryFromJsValue trait. Each tagged field of the enum is attempted in succession until one succeeds.

This is based to #4714 which extends all types into this runtime check, allowing for this implementation.

With this, it is possible to type JS functions like:

export function foo(a: "constant-string" | string | number | BigInt);

where we can write:

#[wasm_bindgen]
enum AEnum {
  ConstantString,
  String(String),
  Number(u32),
  BigInt(U64)
}

and have it work correctly for both directions of bindgen.

@guybedford guybedford force-pushed the enum-fallbacks branch 2 times, most recently from 3f4ca60 to 383f625 Compare October 14, 2025 16:33
@RReverser RReverser requested review from a team and daxpedda October 15, 2025 19:44
@RReverser
Copy link
Member

Just to reflect some private concerns here: this seems like an extension of new syntax sugar that I know @daxpedda wanted to try and avoid in the core, so I'm going to defer to him for review.

E.g. once we land this, what if users might want all other Serde enum representations built into wasm-bindgen as well? It seems we might want to answer a question of where we draw a line between built-in serialization and things covered by serde-wasm-bindgen integration before adding features from said subset.

There are definitely valid reasons to add some integrations to the core, at least for optimisations alone as serde-wasm-bindgen doesn't have as much access to type information at compile-time as wasm-bindgen itself does, but we need to make sure we don't paint ourselves in the corner and on the hook for maintenance of even larger API area with all the semver difficulties that wasm-bindgen already comes with.

Maybe there are hooks we could expose instead to make tools like serde-wasm-bindgen more efficient or support more types, so that the core remains lean? I don't know the answer, but something worth exploring.


Offtop: what you're adding here is rather the untagged enums not tagged ones.

@guybedford guybedford changed the title Support runtime union types with tagged enums Support runtime union types with untagged enums Oct 21, 2025
Copy link
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2088 and #2407 for previous discussions.

I definitely have the concerns pointed out by RReverser, these are also laid out more in #2088.

However, I don't think its realistic that we are getting any sort of better output customization in wasm-bindgen in this version, because its just going be too difficult to design. Maybe we can live with an ad-hoc implementation just for this version, but I'm apprehensive about it.

@guybedford did you ever take a look at tsify? Again, I don't think wasm-bindgen is offering something here, in the end its all a JsValue and we aren't providing an optimized conversion. The TS part can be handled with typescript_type.

I think it would be good to discuss this in the upcoming meeting and how we want to proceed in general.

@guybedford guybedford force-pushed the enum-fallbacks branch 3 times, most recently from f0adc28 to 771867a Compare November 22, 2025 01:23
@guybedford guybedford force-pushed the enum-fallbacks branch 2 times, most recently from 17c5a0a to b114197 Compare December 1, 2025 06:15
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 1, 2025

CodSpeed Performance Report

Merging #4734 will not alter performance

Comparing enum-fallbacks (0f9ca45) with main (96f3e1e)

Summary

✅ 4 untouched

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.