Skip to content

Feature request: Support optional flattened enums #461

@4dawit

Description

@4dawit

Is your feature request related to a problem? Please describe.
Currently, inline_flattened() under impl<T: TS> TS for Option<T> panics and says this can't be flattened

panic!("{} cannot be flattened", <Self as crate::TS>::name())

Describe the solution you'd like
From this example:

#[derive(TS, Serialize)]
#[serde(rename_all = "camelCase")]
enum Enum {
    FirstOption(String),
    SecondOption(bool)
}

#[derive(TS, Serialize)]
struct T {
    a: String,
    #[serde(flatten)]
    flattened_field: Enum // I want this to be Option<Enum>
}

Non optional flattened enums get parsed to something like:

{ a: string } & ({ "firstOption": string } | { "secondOption": bool })

My guess is that adding ? to the enum keys creates intended behavior (either accepts one option because of | or allows undefined if none are given):

{ a: string } & ({ "firstOption"?: string } | { "secondOption"?: boolean })

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions