From a25875c6ee1c90e167dafcc74fa66be41ec580d2 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Wed, 24 Jan 2024 17:24:30 -0300 Subject: [PATCH] Change declarations to use type instead of interface --- macros/src/types/named.rs | 2 +- ts-rs/tests/chrono.rs | 2 +- ts-rs/tests/export_manually.rs | 16 ++++------------ ts-rs/tests/generics.rs | 26 +++++++++++++------------- ts-rs/tests/indexmap.rs | 2 +- ts-rs/tests/lifetimes.rs | 4 ++-- ts-rs/tests/list.rs | 2 +- ts-rs/tests/ranges.rs | 2 +- ts-rs/tests/raw_idents.rs | 2 +- ts-rs/tests/serde-skip-with-default.rs | 2 +- ts-rs/tests/union_with_data.rs | 4 ++-- ts-rs/tests/unsized.rs | 2 +- 12 files changed, 29 insertions(+), 37 deletions(-) diff --git a/macros/src/types/named.rs b/macros/src/types/named.rs index 87861243a..1a3249b42 100644 --- a/macros/src/types/named.rs +++ b/macros/src/types/named.rs @@ -45,7 +45,7 @@ pub(crate) fn named( #fields, ) }, - decl: quote!(format!("interface {}{} {}", #name, #generic_args, Self::inline())), + decl: quote!(format!("type {}{} = {}", #name, #generic_args, Self::inline())), inline_flattened: Some(fields), name: name.to_owned(), dependencies, diff --git a/ts-rs/tests/chrono.rs b/ts-rs/tests/chrono.rs index cc52784ca..d9bc9eeaf 100644 --- a/ts-rs/tests/chrono.rs +++ b/ts-rs/tests/chrono.rs @@ -27,6 +27,6 @@ fn chrono() { assert_eq!( Chrono::decl(), - "interface Chrono { date: [string, string, string, string], time: string, date_time: [string, string, string, string], duration: string, month: string, weekday: string, }" + "type Chrono = { date: [string, string, string, string], time: string, date_time: [string, string, string, string], duration: string, month: string, weekday: string, }" ) } diff --git a/ts-rs/tests/export_manually.rs b/ts-rs/tests/export_manually.rs index aaae5cf56..33a7af12c 100644 --- a/ts-rs/tests/export_manually.rs +++ b/ts-rs/tests/export_manually.rs @@ -27,16 +27,12 @@ fn export_manually() { let expected_content = if cfg!(feature = "format") { concat!( "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n", - "export interface User {\n", - " name: string;\n", - " age: number;\n", - " active: boolean;\n", - "}\n" + "export type User = { name: string; age: number; active: boolean };\n" ) } else { concat!( "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", - "\nexport interface User { name: string, age: number, active: boolean, }" + "\nexport type User = { name: string, age: number, active: boolean, }" ) }; @@ -52,16 +48,12 @@ fn export_manually_dir() { let expected_content = if cfg!(feature = "format") { concat!( "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n", - "export interface UserDir {\n", - " name: string;\n", - " age: number;\n", - " active: boolean;\n", - "}\n" + "export type UserDir = { name: string; age: number; active: boolean };\n" ) } else { concat!( "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", - "\nexport interface UserDir { name: string, age: number, active: boolean, }" + "\nexport type UserDir = { name: string, age: number, active: boolean, }" ) }; diff --git a/ts-rs/tests/generics.rs b/ts-rs/tests/generics.rs index 5dad9853b..cd8cd6875 100644 --- a/ts-rs/tests/generics.rs +++ b/ts-rs/tests/generics.rs @@ -60,27 +60,27 @@ declare! { fn test() { assert_eq!( TypeGroup::decl(), - "interface TypeGroup { foo: Array, }", + "type TypeGroup = { foo: Array, }", ); assert_eq!( Generic::<()>::decl(), - "interface Generic { value: T, values: Array, }" + "type Generic = { value: T, values: Array, }" ); assert_eq!( GenericAutoBound::<()>::decl(), - "interface GenericAutoBound { value: T, values: Array, }" + "type GenericAutoBound = { value: T, values: Array, }" ); assert_eq!( GenericAutoBound2::<()>::decl(), - "interface GenericAutoBound2 { value: T, values: Array, }" + "type GenericAutoBound2 = { value: T, values: Array, }" ); assert_eq!( Container::decl(), - "interface Container { foo: Generic, bar: Array>, baz: Record>, }" + "type Container = { foo: Generic, bar: Array>, baz: Record>, }" ); } @@ -142,7 +142,7 @@ fn generic_struct() { assert_eq!( Struct::<()>::decl(), - "interface Struct { a: T, b: [T, T], c: [T, [T, T]], d: Array, e: Array<[T, T]>, f: Array, g: Array>, h: Array>, }" + "type Struct = { a: T, b: [T, T], c: [T, [T, T]], d: Array, e: Array<[T, T]>, f: Array, g: Array>, h: Array>, }" ) } @@ -164,10 +164,10 @@ fn inline() { t: Generic, } - assert_eq!(Generic::<()>::decl(), "interface Generic { t: T, }"); + assert_eq!(Generic::<()>::decl(), "type Generic = { t: T, }"); assert_eq!( Container::decl(), - "interface Container { g: Generic, gi: { t: string }, t: string, }" + "type Container = { g: Generic, gi: { t: string }, t: string, }" ); } @@ -177,7 +177,7 @@ fn default() { struct A { t: T, } - assert_eq!(A::<()>::decl(), "interface A { t: T, }"); + assert_eq!(A::<()>::decl(), "type A = { t: T, }"); #[derive(TS)] struct B>> { @@ -185,7 +185,7 @@ fn default() { } assert_eq!( B::<()>::decl(), - "interface B | null> { u: U, }" + "type B | null> = { u: U, }" ); assert!(B::<()>::dependencies().iter().any(|dep| dep.ts_name == "A")); @@ -200,7 +200,7 @@ fn default() { // #[ts(inline)] // xi2: X } - assert_eq!(Y::decl(), "interface Y { a1: A, a2: A, }") + assert_eq!(Y::decl(), "type Y = { a1: A, a2: A, }") } #[test] @@ -209,7 +209,7 @@ fn trait_bounds() { struct A { t: T, } - assert_eq!(A::::decl(), "interface A { t: T, }"); + assert_eq!(A::::decl(), "type A = { t: T, }"); #[derive(TS)] struct B(T); @@ -232,5 +232,5 @@ fn trait_bounds() { t: [T; N], } - assert_eq!(D::<&str, 41>::decl(), "interface D { t: Array, }") + assert_eq!(D::<&str, 41>::decl(), "type D = { t: Array, }") } diff --git a/ts-rs/tests/indexmap.rs b/ts-rs/tests/indexmap.rs index 8b7b05e2e..7c8eb208c 100644 --- a/ts-rs/tests/indexmap.rs +++ b/ts-rs/tests/indexmap.rs @@ -14,6 +14,6 @@ fn indexmap() { assert_eq!( Indexes::decl(), - "interface Indexes { map: Record, set: Array, }" + "type Indexes = { map: Record, set: Array, }" ) } diff --git a/ts-rs/tests/lifetimes.rs b/ts-rs/tests/lifetimes.rs index 835610e96..10abf371e 100644 --- a/ts-rs/tests/lifetimes.rs +++ b/ts-rs/tests/lifetimes.rs @@ -8,7 +8,7 @@ fn contains_borrow() { s: &'a str, } - assert_eq!(S::decl(), "interface S { s: string, }") + assert_eq!(S::decl(), "type S = { s: string, }") } #[test] @@ -29,6 +29,6 @@ fn contains_borrow_type_args() { assert_eq!( A::decl(), - "interface A { a: Array, b: Array>, c: Record, }" + "type A = { a: Array, b: Array>, c: Record, }" ); } diff --git a/ts-rs/tests/list.rs b/ts-rs/tests/list.rs index 65aaa00fc..dfa31261b 100644 --- a/ts-rs/tests/list.rs +++ b/ts-rs/tests/list.rs @@ -10,6 +10,6 @@ fn list() { assert_eq!( List::decl(), - "interface List { data: Array | null, }" + "type List = { data: Array | null, }" ); } diff --git a/ts-rs/tests/ranges.rs b/ts-rs/tests/ranges.rs index ac99070ae..10cdcd659 100644 --- a/ts-rs/tests/ranges.rs +++ b/ts-rs/tests/ranges.rs @@ -19,7 +19,7 @@ struct RangeTest { fn range() { assert_eq!( RangeTest::decl(), - "interface RangeTest { a: { start: number, end: number, }, b: { start: string, end: string, }, c: { start: { start: number, end: number, }, end: { start: number, end: number, }, }, d: { start: number, end: number, }, e: { start: Inner, end: Inner, }, }" + "type RangeTest = { a: { start: number, end: number, }, b: { start: string, end: string, }, c: { start: { start: number, end: number, }, end: { start: number, end: number, }, }, d: { start: number, end: number, }, e: { start: Inner, end: Inner, }, }" ); assert_eq!( RangeTest::dependencies(), diff --git a/ts-rs/tests/raw_idents.rs b/ts-rs/tests/raw_idents.rs index 51ee787b8..9fae87952 100644 --- a/ts-rs/tests/raw_idents.rs +++ b/ts-rs/tests/raw_idents.rs @@ -15,6 +15,6 @@ fn raw_idents() { let out = ::decl(); assert_eq!( out, - "interface enum { type: number, use: number, struct: number, let: number, enum: number, }" + "type enum = { type: number, use: number, struct: number, let: number, enum: number, }" ); } diff --git a/ts-rs/tests/serde-skip-with-default.rs b/ts-rs/tests/serde-skip-with-default.rs index 6287f36eb..5f970af0b 100644 --- a/ts-rs/tests/serde-skip-with-default.rs +++ b/ts-rs/tests/serde-skip-with-default.rs @@ -22,6 +22,6 @@ pub struct Foobar { fn serde_skip_with_default() { assert_eq!( Foobar::decl(), - "interface Foobar { something_else: number, }" + "type Foobar = { something_else: number, }" ); } diff --git a/ts-rs/tests/union_with_data.rs b/ts-rs/tests/union_with_data.rs index 2c6973947..b35f3684a 100644 --- a/ts-rs/tests/union_with_data.rs +++ b/ts-rs/tests/union_with_data.rs @@ -24,10 +24,10 @@ enum SimpleEnum { #[test] fn test_stateful_enum() { - assert_eq!(Bar::decl(), r#"interface Bar { field: number, }"#); + assert_eq!(Bar::decl(), r#"type Bar = { field: number, }"#); assert_eq!(Bar::dependencies(), vec![]); - assert_eq!(Foo::decl(), r#"interface Foo { bar: Bar, }"#); + assert_eq!(Foo::decl(), r#"type Foo = { bar: Bar, }"#); assert_eq!( Foo::dependencies(), vec![Dependency::from_ty::().unwrap()] diff --git a/ts-rs/tests/unsized.rs b/ts-rs/tests/unsized.rs index 61c0a26e3..bfddf75ec 100644 --- a/ts-rs/tests/unsized.rs +++ b/ts-rs/tests/unsized.rs @@ -15,6 +15,6 @@ fn contains_str() { assert_eq!( S::decl(), - "interface S { b: string, c: string, r: string, a: string, }" + "type S = { b: string, c: string, r: string, a: string, }" ) }