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

Exporting enum discriminants #153

Closed
alexwaeseperlman opened this issue Jun 21, 2023 · 3 comments
Closed

Exporting enum discriminants #153

alexwaeseperlman opened this issue Jun 21, 2023 · 3 comments

Comments

@alexwaeseperlman
Copy link

I would like to export a struct that looks like this

#[derive(Serialize, Deserialize, Debug, Clone, TS)]
#[ts(export)]
pub enum BuildStatus {
    Unqueued = -1,
    Queued = 0,
    Building = 1,
    BuildFailed = 2,
    TestGameFailed = 3,
}

into a format that looks like this:

export enum BuildStatus {
    Unqueued = -1,
    Queued = 0,
    Building = 1,
    BuildFailed = 2,
    TestGameFailed = 3,
}

This is what was generated:

export type BuildStatus = "Unqueued" | "Queued" | "BuildSucceeded" | "TestGameSucceeded" | "BuildFailed" | "TestGameFailed";

Is there a way to get an enum in typescript with the same discriminants as in rust?

@flukejones
Copy link

I'm in need of this also

@raine
Copy link

raine commented Aug 12, 2023

I thought I would need this but it turned out that the enum values are serialized to their string representation when sent as JSON response from axum. So I can use the literal string representation in the TypeScript code, Rust enum in rust code, and in the database the enum is stored as an integer.

#[repr(i32)]
#[derive(Hash, Eq, Debug, PartialEq, sqlx::Type, TS, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub enum MyEnum {
    Foo = 1,
    Bar = 2,
}

@escritorio-gustavo
Copy link
Contributor

Duplicate of #23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants