Skip to content

Commit

Permalink
Allow for flattening of generic parameters (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxCode authored Jun 28, 2024
1 parent 96487d4 commit af15ff9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 43 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
### Features
### Fixes

# 9.0.1
### Fixes
- Allow using `#[ts(flatten)]` on fields using generic parameters ([#336](https://github.com/Aleph-Alpha/ts-rs/pull/336))


# 9.0.0

### Breaking
Expand Down
78 changes: 39 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ts-rs-macros"
version = "9.0.0"
version = "9.0.1"
authors = ["Moritz Bischof <moritz.bischof1@gmail.com>"]
edition = "2021"
description = "derive macro for ts-rs"
Expand Down
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl DerivedTS {
type WithoutGenerics = #generics;
fn name() -> String { stringify!(#generics).to_owned() }
fn inline() -> String { panic!("{} cannot be inlined", #name) }
fn inline_flattened() -> String { panic!("{} cannot be flattened", #name) }
fn inline_flattened() -> String { stringify!(#generics).to_owned() }
fn decl() -> String { panic!("{} cannot be declared", #name) }
fn decl_concrete() -> String { panic!("{} cannot be declared", #name) }
}
Expand Down
4 changes: 2 additions & 2 deletions ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ts-rs"
version = "9.0.0"
version = "9.0.1"
authors = ["Moritz Bischof <moritz.bischof1@gmail.com>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -42,7 +42,7 @@ chrono = { version = "0.4", features = ["serde"] }

[dependencies]
heapless = { version = ">= 0.7, < 0.9", optional = true }
ts-rs-macros = { version = "=9.0.0", path = "../macros" }
ts-rs-macros = { version = "=9.0.1", path = "../macros" }
dprint-plugin-typescript = { version = "0.90", optional = true }
chrono = { version = "0.4", optional = true }
bigdecimal = { version = ">= 0.0.13, < 0.5", features = [
Expand Down
49 changes: 49 additions & 0 deletions ts-rs/tests/integration/generics_flatten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use ts_rs_macros::TS;

// https://github.com/Aleph-Alpha/ts-rs/issues/335
#[derive(TS)]
#[ts(export, export_to = "generics/flatten/")]
struct Item<D> {
id: String,
#[ts(flatten)]
inner: D,
}

#[derive(TS)]
#[ts(export, export_to = "generics/flatten/")]
struct TwoParameters<A, B> {
id: String,
#[ts(flatten)]
a: A,
#[ts(flatten)]
b: B,
ab: (A, B),
}

#[derive(TS)]
#[ts(export, export_to = "generics/flatten/")]
enum Enum<A, B> {
A {
#[ts(flatten)]
a: A,
},
B {
#[ts(flatten)]
b: B,
},
AB(A, B),
}

#[test]
fn flattened_generic_parameters() {
use ts_rs::TS;

#[derive(TS)]
struct Inner {
x: i32,
}

assert_eq!(Item::<()>::decl(), "type Item<D> = { id: string, } & D;");
assert_eq!(TwoParameters::<(), ()>::decl(), "type TwoParameters<A, B> = { id: string, ab: [A, B], } & A & B;");
assert_eq!(Enum::<(), ()>::decl(), "type Enum<A, B> = { \"A\": A } | { \"B\": B } | { \"AB\": [A, B] };");
}
1 change: 1 addition & 0 deletions ts-rs/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod flatten;
mod generic_fields;
mod generic_without_import;
mod generics;
mod generics_flatten;
mod hashmap;
mod hashset;
mod imports;
Expand Down

0 comments on commit af15ff9

Please sign in to comment.