Skip to content
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

Support for methods ? #40

Open
Vexcited opened this issue Jul 28, 2024 · 2 comments
Open

Support for methods ? #40

Vexcited opened this issue Jul 28, 2024 · 2 comments

Comments

@Vexcited
Copy link

Exactly the same issue as in madonoharu#39. Will this fork support it ?

@siefkenj
Copy link
Owner

I believe wasm_bindgen exports types for struct methods already. Is there a reason you don't use its types?

@Vexcited
Copy link
Author

I find tsify exporting types better than wasm_bindgen, for example in the following rust code :

use crate::models::PaymentMethod;
use serde::{Serialize, Deserialize, Deserializer};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

pub fn parse_payment_methods<'de, D>(deserializer: D) -> Result<Vec<PaymentMethod>, D::Error>
where
  D: Deserializer<'de>,
{
  let s = Vec::<serde_json::Value>::deserialize(deserializer)?;
  Ok(
    s.iter()
      .map(|v| PaymentMethod::from_api(v["name"].as_str().unwrap()))
      .collect(),
  )
}

#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter_with_clone))]
pub struct Restaurant {
  // ...
  #[serde(
    rename(deserialize = "payment"),
    deserialize_with = "parse_payment_methods"
  )]
  pub payment_methods: Vec<PaymentMethod>,
  // ...
}

I get this .d.ts output :

export class Restaurant {
  // ...
  payment_methods: any[];
  // ...
}

Every other properties are correct except this one and there's no reason why. I tried to run the exact same code with tsify and it outputted the type correctly, except all the methods got stripped away.

Also I like the fact that you can console.log(any_object_from_rust) directly and it shows you the object properties instead of only the class name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants