Comments (2)
I've also bumped in this issue. To get C-style / ambient enum declarations, you'd need the serde_repr
crate and something like:
use serde_repr::{Deserialize_repr};
#[derive(Debug, PartialEq, Eq, Deserialize_repr, Tsify)]
#[tsify(from_wasm_abi)]
#[repr(u8)]
pub enum AmbientEnum {
Int32 = 0,
Int64 = 1,
}
This way, you'd get the correct Rust deserialization logic:
#[wasm_bindgen_test]
fn ambient_enum_test() {
// Example deserialization code
let json_data = r#"0"#;
let n_value = serde_json::from_str::<u32>(&json_data).unwrap();
let n_value = serde_json::from_str::<i32>(&json_data).unwrap();
let n_value = serde_json::from_str::<u64>(&json_data).unwrap();
let n_value = serde_json::from_str::<i64>(&json_data).unwrap();
let ambient_enum = serde_json::from_str::<AmbientEnum>(&json_data).unwrap();
assert_eq!(ambient_enum, AmbientEnum::Int32);
}
However, tsify
would be confused about the types, and would generate
export type AmbientEnum = "Int32" | "Int64";
rather than
export enum AmbientEnum {
Int32 = 0,
Int64 = 1,
}
@madonoharu would you have opinionated ideas about how to solve this? Should tsify
be able to recognise #[repr(u8)]
declarations on enums, and apply different type generations as per the example above?
from tsify.
This is the same use case I stumbled upon, thank you all for sharing such examples.
Even if not strictly the same, as a workaround I used enum with namespace.
from tsify.
Related Issues (20)
- Crate version not in sync HOT 1
- Trait `From<...>` is not implemented for `JsValue` when returning structs in async functions HOT 2
- Allow Passing Options to serde_wasm_bindgen
- Support serde_bytes::ByteBuf
- Customizing struct name / namespace? HOT 1
- Bug: Fields starting with numeric literal need to be quoted HOT 1
- Support for methods?
- Should we fork this to a more community driven repository?
- Rust-analyzer marks structs with snake-case warning HOT 7
- Invalid references created with `serde(rename = ...)`
- Entire Wasm instance is invalidated if serde impl from macro crashes on deserialization step HOT 2
- Returning Vec<T> where T: IntoWasmAbi HOT 2
- Tsify doesn't generate typescript definitions under certain conditions HOT 3
- FR: expand rename behaviour when only `into` / `from` + implemented with `rename_all`
- Support untagged variant
- Cannot return Tsify types from async methods with wasm-bindgen-futures HOT 4
- Feature request: `tsify(required)`
- Allow custom `DECL` TypeScript definition string
- No method named `to_string` found for struct with `from_wasm_abi` HOT 1
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 tsify.