Skip to content

TS Export Panics When Flattening Zero-Field Struct #473

@sinder38

Description

@sinder38

Describe the bug

When using #[serde(flatten)] with a zero-field struct that derives TS, the TypeScript export generation panics with the error: "[StructName here] cannot be flattened".

To Reproduce

Steps to reproduce the behavior:

Create a zero-field struct with `TS`:
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct Inner {}

// Create a parent struct that flattens the zero-field struct:
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct Outer {
    #[serde(flatten)]
    pub inner: Inner,
    pub other_field: String,
}
  1. Run TypeScript export
  2. See panic: "Inner cannot be flattened"
  3. Surprised pikachu

Expected behavior

The zero-field struct should be flattened without issue, resulting in TypeScript output equivalent to:

export type Outer {
    other_field: string;
}

Or at least the library should panic indicating that zero-field structs cannot be flattened.

Version

12.0.1

Workaround:

I could just remove the whole struct and it's flatten.

Why?

That empty struct is a placeholder which could change at any moment I don't want to remove it even though it is empty

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions