-
Notifications
You must be signed in to change notification settings - Fork 117
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
Is it possible to generate numeric enums? #140
Comments
I guess this is how enums are handled in JSON. Not an issue with ts-rs. |
@ottosson How do you solve this? |
@percy507 in my case I didn't have to use the numbers really, but could use the string representation of the enum in TS. Not optimal but it works. You can convert the enum value to number with serde so I think it should be supported by ts-rs. |
@ottosson Thanks. I also solve this. I don't use 1. use `serde_repr` crate
2. modify the code
- `#[derive(Serialize, Deserialize, TS, Debug)]` to `#[derive(Serialize_repr, Deserialize_repr, TS, Debug)]` |
@percy507 Sadly, this doesn't work. While the rust part works, the generated bindings result in the same format as the other example: use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use ts_rs::TS;
#[derive(Serialize, Deserialize, TS, Debug)]
#[ts(export)]
pub struct ErrorResponse {
pub error_code: ErrorCode,
pub message: String,
}
#[repr(i32)]
#[derive(Serialize_repr, Deserialize_repr, TS, Debug)]
#[ts(export)]
pub enum ErrorCode {
// User defined error codes starts from 1000
UserNotFound = 1000,
InvalidCredentials = 1001,
// Generic error codes starts from 1
InvalidRequest = 1,
ParseError = 2,
NetworkError = 3,
} Binding: // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type ErrorCode = "UserNotFound" | "InvalidCredentials" | "InvalidRequest" | "ParseError" | "NetworkError";
Is this possible with the current version? @ottosson, Could you reopen this issue? Otherwise, I'll open a new issue. |
As stated before, we really dislike TS enums (This comment is a very thorough explanation on the reasons to prefer type unions over TS enums), but we could try to figrure out some way to work with In the meantime you could do use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use ts_rs::TS;
#[derive(Serialize, Deserialize, TS, Debug)]
#[ts(export)]
pub struct ErrorResponse {
pub error_code: ErrorCode,
pub message: String,
}
#[repr(i32)]
#[derive(Serialize_repr, Deserialize_repr, TS, Debug)]
#[ts(export, type = "1000 | 1001 | 1 | 2 | 3")]
pub enum ErrorCode {
// User defined error codes starts from 1000
UserNotFound = 1000,
InvalidCredentials = 1001,
// Generic error codes starts from 1
InvalidRequest = 1,
ParseError = 2,
NetworkError = 3,
} Which should generate export type ErrorCode = 1000 | 1000 | 1 | 2 | 3; |
Hey! While I share @gustavo-shigueo's sentiment, i still think supporting this would be pretty cool. |
Maybe we could have it as We could create a new |
I don't know how this crate works internally, but in the meantime, I'm using |
Hi. thanks for this awesome library!
Is it possible to generate numeric enums in typescript from my rust enums?
becomes
I would expect it to become something like
Is this possible?
The text was updated successfully, but these errors were encountered: