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

serde(skip_serializing_if=...) is not annotated as optional (?) in TypeScript definition #300

Open
Vlad-Shcherbina opened this issue Dec 18, 2024 · 2 comments

Comments

@Vlad-Shcherbina
Copy link
Contributor

[dependencies]
serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.133"
specta = { version = "2.0.0-rc.20", features = ["derive"]}
specta-typescript = { version = "0.0.7" }
fn is_false(b: &bool) -> bool { !*b }

#[derive(serde::Serialize, specta::Type)]
struct MyStruct {
    #[serde(skip_serializing_if = "is_false")]
    flag: bool,
}

fn main() {
    println!("{}", specta_typescript::export::<MyStruct>(&specta_typescript::Typescript::new()).unwrap());
    println!("{}", serde_json::to_string(&MyStruct { flag: false }).unwrap());
}

I'm getting this:

export type MyStruct = { flag: boolean }
{}

Expected:

export type MyStruct = { flag?: boolean }
{}
@oscartbeaumont
Copy link
Member

Duplicate of #233 and definitely a great idea!

Happy for a PR if you wanted to give it a shot!

@oscartbeaumont
Copy link
Member

oscartbeaumont commented Dec 18, 2024

The most difficult part of this is that the types when serializing and deserializing become different. Specta currently doesn't have a way to express that and long term we are going to need to have that but as a stop gap solution we can just do the most restrictive of the two.

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