Aleph-Alpha / ts-rs

Generate TypeScript bindings from Rust types
MIT License
989 stars 99 forks source link

`ts(optional)` should be working on any which has `skip_serializing_if` #326

Closed xiao-e-yun closed 1 month ago

xiao-e-yun commented 1 month ago

Describe the bug ts(optional) can't support skip_serializing_if attr

To Reproduce

  1. Write this code
    #[ts(export)]
    #[derive(TS , Serialize)]
    pub struct A{
    #[ts(optional)]
    #[serde(skip_serializing_if = "<[_]>::is_empty")]
    pub arrays: Vec<String>,
    }
  2. Throw error: optional can only be used on an Option type

Expected behavior return type

export type A = { arrays?: Array<string>, };
NyxCode commented 1 month ago

On 8.1.0, you can use

    #[ts(as = "Option<Vec<String>>", optional)]
    my_vec: Vec<String>

On the latest master (and soon in 9.0.0), you can then do

    #[ts(as = "Option<_>", optional)]
    my_vec: Vec<String>

It is by design that the TS types we generate match the type in Rust. This has been discussed at length before, see #175.