-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to generate a TS ambient enum? #25
Comments
I've also bumped in this issue. To get C-style / ambient enum declarations, you'd need the 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, 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 |
This is the same use case I stumbled upon, thank you all for sharing such examples. |
Edit: I'm generalizing this issue.
Currently all rust enums are generated as typescript union types. While this is perfectly valid as rust enums are literally discriminated unions, sometimes we'd expect plain old
enum
s in the declaration file. Apparently the d.ts way is to use ambient enum. Can we do this with tsify?The text was updated successfully, but these errors were encountered: