Skip to content

Suggestion for #[serde(flatten)] #114

@Roms1383

Description

@Roms1383

Hi, just tried your library for the first time and really like it, also love its simplicity.

What lead me here is that I tried #[serde(flatten)] and got the error.
Here's one suggestion that would work pretty well IMHO, for serde struct flattening,
given for example this code:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[typeshare]
pub struct Geolocation {
    pub lat: f32,
    pub long: f32,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[typeshare]
pub struct Meta {
    #[serde(rename = "createdAt")]
    pub created_at: chrono::DateTime<chrono::Utc>,
    #[serde(rename = "updatedAt")]
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[typeshare]
pub struct User {
    email: String,
    #[serde(flatten)]
    geo: Geolocation,
    #[serde(flatten)]
    meta: Meta,
}

would generate:

export interface Geolocation {
	lat: number;
	long: number;
}

export interface Meta {
	createdAt: Date;
	updatedAt: Date;
}

export interface User extends Geolocation, Meta {
	email: string;
}

I thought that would be a pretty good fit, especially since in Typescript an interface can inherit multiple interfaces, and it also does flatten the nested fields in the parent definition.
Let me know what you think, thanks for reading me :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions