You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ran typeshare on my project and got the following errors:
thread '<unnamed>' panicked at 'failed to parse a rust type: ["usize"]', /Users/affan/.cargo/registry/src/github.com-1ecc6299db9ec823/typeshare-cli-1.0.0/src/main.rs:209:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread '<unnamed>' panicked at 'failed to parse a rust type: ["i64"]', /Users/affan/.cargo/registry/src/github.com-1ecc6299db9ec823/typeshare-cli-1.0.0/src/main.rs:209:17
i64 can be represented using a JS BigInt.
Reacted by Jeremy Chone, Josh Holmer, Peter Vrba, Philippe Loctaux, Cathal, Benoît Jeaurond, Jane Lewis, Paweł Czochański, Max, Harrison Hogg and 13 more
But the types exist in the SpecialRustType and looking in the four supported languages, they all have mappings for those four types.
Locally I've changed those three lines in rust_types.rs to parse the types and I'm not getting any errors, but I'm guessing that there must be a reason that those lines/errors exist? It works for Go, Swift, TypeScript, and Kotlin.
I think part of the problem is that JSON is usually going to be the medium of exchange for typeshared data, and so when you're loading into javascript / typescript you'd need to not use JSON.parse, since it doesn't care what type you intended and will just load into a number. A lot of the complexity around typeshare enums is similarly related to conforming to the default behavior of various languages when dealing with their equivalent of enums.
I understand that most people would use JSON (as it is one of the most popular transport medium), but it is not the only transport medium in which typeshare types can be used, e.g. FFIs
If users of typeshare decide to use it with 64bit types, maybe the library should just give a warning and maybe suggest an alternative library instead of the builtin JSON.parse/JSON.stringify, such as https://github.com/blitz-js/superjson, for the TypeScript/JavaScript side? It seems like this feature is blocked on the fact that JavaScript does not play well by default with 64bit values in JSON, whilst other languages are fine.
Reacted by Cathal, Adam McQuilkin, Eran Boodnero and alex
Since typeshare is a developer tool, I would recommend picking a default (failing or bigint are both reasonable) and letting the developer override it by property.
In many circumstances, 2^53-1 or 2^64-1 can be considered equivalent (v.s. the complexity of introducing bigint in all JS code base). Obviously, this should not be the default, but not providing this escape hatch might add unnecessary code complexity downstream.
Reacted by Eran Boodnero, stefnotch, Austin Tasato, Carson McManus, Marcus Ramse, BlueGreenMagick and alex
I just ran into this -- I also need to serialize u64 types. Seems like an oversight to always panic when encountering these types as there are types in the destination languages to support large numbers. At least let the developer set the type themselves.
@LuminaSapphiraI made a PR for supporting this about a year back. What do you think of that approach (i.e. allow only if the output type is explicitly specified?)
Activity
jeremychone commentedon Nov 28, 2022
as with
ts-rswould be nice to have a#[typeshare(type = "number")]property attribute.snowsignal commentedon Dec 1, 2022
Thanks for opening this issue. I'll look into whether this is possible.
BenJeau commentedon Dec 31, 2022
Getting a similar error with
u64andisize, those four types seems to be "disabled" heretypeshare/core/src/rust_types.rs
Lines 225 to 227 in fbf5aea
But the types exist in the SpecialRustType and looking in the four supported languages, they all have mappings for those four types.
Locally I've changed those three lines in
rust_types.rsto parse the types and I'm not getting any errors, but I'm guessing that there must be a reason that those lines/errors exist? It works for Go, Swift, TypeScript, and Kotlin.Changes
In
rust_types.rs:and in
typescript.rs:SpecialRustType::U64 | SpecialRustType::I64 | SpecialRustType::ISize | SpecialRustType::USize => { - panic!("64 bit types not allowed in Typeshare") + Ok("number".into()) }Lucretiel commentedon Jan 4, 2023
I think part of the problem is that JSON is usually going to be the medium of exchange for typeshared data, and so when you're loading into javascript / typescript you'd need to not use
JSON.parse, since it doesn't care what type you intended and will just load into anumber. A lot of the complexity around typeshare enums is similarly related to conforming to the default behavior of various languages when dealing with their equivalent of enums.BenJeau commentedon Jan 5, 2023
I understand that most people would use JSON (as it is one of the most popular transport medium), but it is not the only transport medium in which
typesharetypes can be used, e.g. FFIsIf users of
typesharedecide to use it with 64bit types, maybe the library should just give a warning and maybe suggest an alternative library instead of the builtinJSON.parse/JSON.stringify, such as https://github.com/blitz-js/superjson, for the TypeScript/JavaScript side? It seems like this feature is blocked on the fact that JavaScript does not play well by default with 64bit values in JSON, whilst other languages are fine.jeremychone commentedon Jan 5, 2023
Since typeshare is a developer tool, I would recommend picking a default (failing or bigint are both reasonable) and letting the developer override it by property.
In many circumstances, 2^53-1 or 2^64-1 can be considered equivalent (v.s. the complexity of introducing bigint in all JS code base). Obviously, this should not be the default, but not providing this escape hatch might add unnecessary code complexity downstream.
mattyg commentedon Feb 18, 2024
I just ran into this -- I also need to serialize u64 types. Seems like an oversight to always panic when encountering these types as there are types in the destination languages to support large numbers. At least let the developer set the type themselves.
AlexanderProd commentedon Jul 22, 2024
@mattyg A workaround for this is to use
#[typeshare(serialized_as = "number")].Uzaaft commentedon Aug 26, 2024
This workaround doesn't work. Tested stable and the beta branch.
LuminaSapphira commentedon Sep 24, 2024
I think it would make sense for us to support these and emit a suppressable warning if they are used. @Lucretiel
tomjw64 commentedon Sep 28, 2024
@LuminaSapphira I made a PR for supporting this about a year back. What do you think of that approach (i.e. allow only if the output type is explicitly specified?)
tomjw64 commentedon Sep 28, 2024
I resolved the existing merge conflicts in the above PR. I would still really appreciate a review + workflow approval from a maintainer!
mjovanc commentedon Apr 2, 2026
Any news on this?