From 95b455f5854a0737cd4be20be910530663dc25ec Mon Sep 17 00:00:00 2001 From: Heliozoa Date: Tue, 15 Aug 2023 09:44:29 +0300 Subject: [PATCH] Check for empty tuple in `format_type` --- macros/src/types/generics.rs | 7 +++++++ ts-rs/tests/unit.rs | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/macros/src/types/generics.rs b/macros/src/types/generics.rs index 0396d9267..290659e32 100644 --- a/macros/src/types/generics.rs +++ b/macros/src/types/generics.rs @@ -71,6 +71,13 @@ pub fn format_type(ty: &Type, dependencies: &mut Dependencies, generics: &Generi // same goes for a tuple (`(A, B, C)`) - it doesn't have a type arg, so we handle it // explicitly here. Type::Tuple(tuple) => { + if tuple.elems.is_empty() { + // empty tuples `()` should be treated as `null` + return super::unit::null(&StructAttr::default(), "") + .unwrap() + .inline; + } + // we convert the tuple field to a struct: `(A, B, C)` => `struct A(A, B, C)` let tuple_struct = super::type_def( &StructAttr::default(), diff --git a/ts-rs/tests/unit.rs b/ts-rs/tests/unit.rs index db2cdba7d..c6fdcf558 100644 --- a/ts-rs/tests/unit.rs +++ b/ts-rs/tests/unit.rs @@ -14,9 +14,14 @@ struct Unit2 {} #[derive(TS)] struct Unit3(); +// serde_json serializes this to `null`, so it's TS type is `null` as well. +#[derive(TS)] +struct Unit4(()); + #[test] fn test() { assert_eq!("type Unit = null;", Unit::decl()); assert_eq!("type Unit2 = Record;", Unit2::decl()); assert_eq!("type Unit3 = never[];", Unit3::decl()); + assert_eq!("type Unit4 = null;", Unit4::decl()); }