Skip to content

Optional types in generated Typescript #76

@hardbyte

Description

@hardbyte

Optional types don't appear to generate correct Typescript types without a serde skip_serializing_if annotation.

#[derive(Debug, Clone, Deserialize, reflectapi::Input)]
#[serde(deny_unknown_fields)]
pub struct ARequest {
    pub reflect_option: reflectapi::Option<String>,

    #[serde(default, skip_serializing_if = "reflectapi::Option::is_undefined")]
    pub annotated_reflect_option: reflectapi::Option<String>,

    pub std_option: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotated_std_option: Option<String>,
}

Produces the following typescript:

interface ARequest {
        reflect_option: string | null | undefined;
        annotated_reflect_option?: string | null | undefined;
        std_option: string | null;
        annotated_std_option?: string | null;
}

Assuming the first option is the recommended way to define optional types in reflectapi, the generated Typescript interface still requires a reflect_option to be included when defining an ARequest:

Image

https://www.typescriptlang.org/play/?#code/FDCWDsBcFMCcDMCGBjaACAggJWgRwK7QDOkaA3sGldVbNPADbTKQD6A9gA6SjvgBcaErAgBzNAB804fAwaS0+cABN6EaMoDclGtUThw7SIhjLWdRszZcefAPyDhYhTLkKlq+Oq07dQyGY2vAL+IuDiUq4M2n5U+obGpqwkgdzBDqHOkbLRwAC+IEykIdh4hCRoALzkIHnaQA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions