Comments (6)
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.
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.
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.
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.
Agreed. The reason I had this issue come was that my rust backend was sending random usize
s to the frontend, and they weren't getting parsed correctly because of the limitations with JS number
s.
from ts-rs.
I see, but beware that even if this gets implemented, you still need to customize your (de)serialization logic to produce bigint
s 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)
- 'internal error: entered unreachable code' when generating struct fields using Results HOT 4
- Bug: rename_all for enums doesn't match serde HOT 4
- `#[derive(TS)]` and associated types HOT 14
- Feature request: `#[ts(bound)]` attribute HOT 13
- Feature request: rust_decimal::Decimal TS implementation HOT 2
- Can we support `#[serde(with = "..")]`? HOT 11
- Feature request: `bnum` compatibility HOT 2
- bug: `impl Trait` only allowed in function and inherent method return types, not in trait method return types HOT 4
- TypeList recursion limit HOT 3
- Feature request: Export in the same file HOT 3
- bug: Using `#[ts(rename_all = "...")]` `snake_case`, `SCREAMING_SNAKE_CASE` and `kebab-case` is not compatible with serde
- Randomly failing `imports` test HOT 1
- Unqualified call to `Self::name()`
- Feature request: `serde-wasm-bindgen` compatibility. HOT 4
- Feature request: How to export multiple types to the same file HOT 4
- Feature request: Typed array types HOT 1
- bug: Tuples cause imports to be missed HOT 5
- bug: files are missing final newlines
- bug: `export::path:absolute` function fails when `TS_RS_EXPORT_DIR` is set to an absolute path and the `path_bug` test is executed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-rs.