Aleph-Alpha/ts-rs

type override in enums only works with types implementing `Serialize`

nitn3lav opened this issue · 2 comments

Type overrides seem to require that the type marked with #[ts(type = "mytype")] implements Serialize. This only happens when using adjacently tagged or internally tagged enums (externally tagged and untagged enums work fine).

use serde::Serialize;
use serde_with::{serde_as, DisplayFromStr};
use ts_rs::TS;

struct Unsupported;

impl Display for Unsupported {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        todo!()
    }
}

#[serde_as]
#[derive(Serialize, TS)]
#[serde(tag = "type", content = "data")]
pub enum TestEnum {
    Variant(
        #[ts(type = "string")]
        #[serde_as(as = "DisplayFromStr")]
        Unsupported,
    ),
}
error[E0277]: the trait bound `error::Unsupported: TS` is not satisfied
  --> src/error.rs:14:21
   |
14 | #[derive(Serialize, TS)]
   |                     ^^ the trait `TS` is not implemented for `error::Unsupported`
   |
   = help: the following other types implement trait `TS`:
             bool
             char
             isize
             i8
             i16
             i32
             i64
             i128
           and 71 others
   = note: this error originates in the derive macro `TS` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `error::Unsupported: TS` is not satisfied
   --> src/error.rs:20:9
    |
20  |         Unsupported,
    |         ^^^^^^^^^^^ the trait `TS` is not implemented for `error::Unsupported`
    |
    = help: the following other types implement trait `TS`:
              bool
              char
              isize
              i8
              i16
              i32
              i64
              i128
            and 71 others
note: required by a bound in `Dependency::from_ty`
   --> /Users/valentinleistner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ts-rs-7.0.0/src/lib.rs:331:23
    |
331 |     pub fn from_ty<T: TS + 'static + ?Sized>() -> Option<Self> {
    |                       ^^ required by this bound in `Dependency::from_ty`

For more information about this error, try `rustc --explain E0277`.

It seems like this has already been fixed on main (I was using version 7.0.0 from crates.io). It would probably make sense to publish a new release? Feel free to close this.

I just published 7.1.0. If the issue persists, please let me know!