Aleph-Alpha / ts-rs

Generate TypeScript bindings from Rust types
MIT License
1.15k stars 115 forks source link

Feature request: `bnum` compatibility #279

Open aumetra opened 8 months ago

aumetra commented 8 months ago

Is your feature request related to a problem? Please describe.

In my exploration in adopting ts-rs for TypeScript typegen in a project, I hit the roadblock of needing a bnum implementation.

Describe the solution you'd like

An implementation for the bnum structs would be nice, but there is something to discuss here, which is why I opened an issue here first.


First off, here's a link to the documentation of the crate: https://docs.rs/bnum/latest/bnum/

While crate is made for arithmetic, it is made for arbitrary precision arithmetic.
Now here's the thing, should the type for bnum be expressed as a number, a string, or some binary format.

The serde impl seems to serialize it as an array of limbs, but in most cases you'd probably want to serialize it as a string and parse it radix 10 or 16.

Personally, I'd express the type as string? Or should bigint be chosen here? (not sure if it's arbitrary precision?)

NyxCode commented 8 months ago

Hey!
If we add an impl for it behind a bnum-impl feature gate, then that impl should be 1:1 compatible with their serde serialization. I think that's the only way that feature would be usefull for the average user.

If you're doing something special for serialization, you can use #[ts(type = "string")] or #[ts(as = "..")].

NyxCode commented 8 months ago

Our impls for large integer types (e.g u128) do currently use bigint, but it was a somewhat painfull tradeoff with them. Serializing them as number would be serde-compatible by default, but probably wrong in almost all cases.

If I had the chance to go back and change it, I'd probably still chose number though. Then, it's serde-compatible, and when users chose a way of serializing them, they can also change the TS type using #[ts(type = "..")] or #[ts(as = "..")].