#[non_exhaustive]pub struct Error { /* private fields */ }Expand description
The error type for the TypeScript exporter.
§BigInt Forbidden
Specta Typescript intentionally forbids exporting BigInt-style Rust integer types. This includes usize, isize, i64, u64, u128, i128 and f128.
This guard exists because JSON.parse will truncate large integers to fit into a JavaScript number type so we explicitly forbid exporting them.
We take the stance that correctness matters more than developer experience as people using Rust generally strive for correctness.
If you encounter this error, there are a few common migration paths (in order of preference):
-
Use a Specta-based framework which can handle these types
- None currently exist but it would theoretically be possible refer to #203 for more information.
-
Use a smaller integer types (any of
u8/i8/u16/i16/u32/i32/f64).- Only possible when the biggest integer you need to represent is small enough to be represented by a
numberin JS. - This approach forces your application code to handle overflow/underflow values explicitly
- Downside is that it can introduce annoying glue code and doesn’t actually work if your need large values.
- Only possible when the biggest integer you need to represent is small enough to be represented by a
-
Serialize the value as a string
- This can be done using
#[specta(type = String)]for the type combined with a Serde#[serde(with = "...")]attribute for runtime. - Downside is that it can introduce annoying glue code, both on in Rust and in JS as you will need to turn it back into a
new BigInt(myString)in JS but this will support numbers of any size losslessly.
- This can be done using
-
UNSAFE: Accept precision loss on per-field basis
- Accept that large numbers may be deserialized differently than they are in Rust and use
#[specta(type = specta_typescript::Number)]to bypass this warning on a per-field basis. - Marking each field explicitly encodes the decision similar to an
unsafeblock, ensuring everyone working on your codebase is aware of the risk and where it exists within the codebase. - This doesn’t work for external implementations like
serde_json::Valuewhich containBigInt’s as you don’t control the definition.
- Accept that large numbers may be deserialized differently than they are in Rust and use
-
UNSAFE: Accept precision loss using
specta_util::Remapper- You can apply a
Remapperto yourTypescollection to override types. This would allow you to remapusize/isize/i64/u64/i128/u128/f128intonumber. - This is highly not recommended but it might be required if your using
serde_json::Valueor other built-in impls which containBigInt’s as you can’t override them. - Refer to discussion around this on #481.
- You can apply a
Implementations§
Source§impl Error
impl Error
Sourcepub fn named_datatype(&self) -> Option<&NamedDataType>
pub fn named_datatype(&self) -> Option<&NamedDataType>
The named Rust type being exported when this error occurred, if known.
Sourcepub fn trace(&self) -> &[ErrorTraceFrame]
pub fn trace(&self) -> &[ErrorTraceFrame]
TypeScript exporter traversal context for this error.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()