Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to generate flattened enum #96

Closed
George-Miao opened this issue Apr 29, 2022 · 3 comments · Fixed by #206
Closed

Unable to generate flattened enum #96

George-Miao opened this issue Apr 29, 2022 · 3 comments · Fixed by #206
Labels
difficulty: medium enhancement New feature or request

Comments

@George-Miao
Copy link
Contributor

George-Miao commented Apr 29, 2022

I have tried both tagged or untagged enum in struct, not working.

Code

Tagged:

use serde::Serialize;
use ts_rs::TS;

#[derive(Serialize, TS)]
#[ts(export)]
struct Foo {
    #[serde(flatten)]
    baz: Bar,
}

#[derive(Serialize, TS)]
#[serde(tag = "tag")]
#[ts(export)]
enum Bar {
    _Baz { a: i32 },
}

Untagged:

use serde::Serialize;
use ts_rs::TS;

#[derive(Serialize, TS)]
#[ts(export)]
struct Foo {
    #[serde(flatten)]
    baz: Bar,
}

#[derive(Serialize, TS)]
#[serde(untagged)]
#[ts(export)]
enum Bar {
    _Baz,
}

Error

---- ts::export_bindings_foo stdout ----
thread 'ts::export_bindings_foo' panicked at 'Bar cannot be flattened', [:REDACTED:]/.cargo/registry/src/github.com-1ecc6299db9ec823/ts-rs-6.1.2/src/lib.rs:243:9

Info

version of ts-rs: 6.1.2
version of rustc: rustc 1.60.0 (7737e0b5c 2022-04-04)

@NyxCode
Copy link
Collaborator

NyxCode commented Apr 29, 2022

thanks for the report! will look into this when I find some time.

@NyxCode NyxCode added bug Something isn't working enhancement New feature or request difficulty: medium and removed bug Something isn't working labels Apr 29, 2022
@ilyvion
Copy link

ilyvion commented Aug 17, 2023

The untagged variant should be fairly easy to do, at least conceptually. If you have something like

#[derive(Serialize, TS)]
#[ts(export)]
struct Foo {
    one: i32,
    two: String,

    #[serde(flatten)]
    baz: Bar,
}

#[derive(Serialize, TS)]
#[serde(untagged)]
#[ts(export)]
enum Bar {
    Baz(BazStruct),
    Qux(QuxStruct),
}

The generated TS for Bar becomes something like

// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { BazStruct } from "./BazStruct";
import type { QuxStruct } from "./QuxStruct";

export type Bar = BazStruct | QuxStruct;

and it feels like the generated TS for Foo could just be

import type { Bar } from "./Bar";

export type Foo = {
    one: number
    two: string
} & Bar;

or thereabouts.

This was referenced Jan 23, 2024
@NyxCode
Copy link
Collaborator

NyxCode commented Jan 26, 2024

Huge shoutout to @escritorio-gustavo for getting this implemented!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants