Giter Site home page Giter Site logo

Comments (6)

escritorio-gustavo avatar escritorio-gustavo commented on June 1, 2024 1

In particular, if you send data to your frontend as JSON, which doesn't support bigint, you'd need to encode your numbers as strings or arrays of bytes, which you'd then need to deserialize into a bigint in your frontend code

from ts-rs.

escritorio-gustavo avatar escritorio-gustavo commented on June 1, 2024

This can be implemented with the following code

#[cfg(target_pointer_width = "16")]
impl_primitives! {
    usize, isize, NonZeroUsize, NonZeroIsize => "number"
}

#[cfg(target_pointer_width = "32")]
impl_primitives! {
    usize, isize, NonZeroUsize, NonZeroIsize => "number"
}

#[cfg(target_pointer_width = "64")]
impl_primitives! {
    usize, isize, NonZeroUsize, NonZeroIsize => "bigint"
}

But people seem to dislike the use of bigint in the first place (#94)

What do you think @NyxCode?

from ts-rs.

escritorio-gustavo avatar escritorio-gustavo commented on June 1, 2024

Alternatively, we could make a manual impl that returns

// 6 bytes is the most that can be guaranteed to fit a JS Number
if std::mem::size_of::<usize>() <= 6 {
    "number"
} else {
    "bigint"
}.to_owned()

This would handle any weird architechture (I'm decently sure there's nothing with 5 byte pointers, but who knows)

from ts-rs.

escritorio-gustavo avatar escritorio-gustavo commented on June 1, 2024

people seem to dislike the use of bigint in the first place

I do agree that this should be the default though. As #94 (comment) states, "soundness should not be an opt-in", users coercing i/u64, i/u128, i/usize to number should do so on purpose and at their own risk

from ts-rs.

lucasvanmol avatar lucasvanmol commented on June 1, 2024

Agreed. The reason I had this issue come was that my rust backend was sending random usizes to the frontend, and they weren't getting parsed correctly because of the limitations with JS numbers.

from ts-rs.

escritorio-gustavo avatar escritorio-gustavo commented on June 1, 2024

I see, but beware that even if this gets implemented, you still need to customize your (de)serialization logic to produce bigints for JS, as just changing the type definition in TS doesn't actually fix the issue. In the meantime, you can use #[ts(type = "bigint")] or #[ts(as = "u64")] (I'd prefer as since it prevents typos by actually parsing and using the type you give it, while type just takes in a string and doesn't check it) as a temporary fix

from ts-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.