Skip to content

Commit

Permalink
Merge pull request #164 from Heliozoa/unit-null
Browse files Browse the repository at this point in the history
Check for empty tuple in `format_type`
  • Loading branch information
NyxCode authored Aug 16, 2023
2 parents 5f21492 + 95b455f commit c4bee20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions macros/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions ts-rs/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, never>;", Unit2::decl());
assert_eq!("type Unit3 = never[];", Unit3::decl());
assert_eq!("type Unit4 = null;", Unit4::decl());
}

0 comments on commit c4bee20

Please sign in to comment.