mirror of
https://github.com/xazukx/ts-rs.git
synced 2026-11-19 22:40:25 +00:00
Change HashMap to export mapped types (#339)
This commit is contained in:
+2
-2
@@ -896,11 +896,11 @@ impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
|
||||
}
|
||||
|
||||
fn name() -> String {
|
||||
format!("{{ [key: {}]: {} }}", K::name(), V::name())
|
||||
format!("{{ [key in {}]?: {} }}", K::name(), V::name())
|
||||
}
|
||||
|
||||
fn inline() -> String {
|
||||
format!("{{ [key: {}]: {} }}", K::inline(), V::inline())
|
||||
format!("{{ [key in {}]?: {} }}", K::inline(), V::inline())
|
||||
}
|
||||
|
||||
fn visit_dependencies(v: &mut impl TypeVisitor)
|
||||
|
||||
+428
-428
@@ -1,428 +1,428 @@
|
||||
#![allow(clippy::box_collection, clippy::enum_variant_names, dead_code)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::{
|
||||
collections::{BTreeMap, HashSet},
|
||||
fmt::Debug,
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Generic<T>
|
||||
where
|
||||
T: TS,
|
||||
{
|
||||
value: T,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericAutoBound<T> {
|
||||
value: T,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericAutoBound2<T>
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
value: T,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Container {
|
||||
foo: Generic<u32>,
|
||||
bar: Box<HashSet<Generic<u32>>>,
|
||||
baz: Box<BTreeMap<String, Rc<Generic<String>>>>,
|
||||
}
|
||||
|
||||
macro_rules! declare {
|
||||
($(#[$meta:meta])* $name:ident { $($fident:ident: $t:ty),+ $(,)? }) => {
|
||||
$(#[$meta])*
|
||||
struct $name {
|
||||
$(pub $fident: $t),+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare! {
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
TypeGroup {
|
||||
foo: Vec<Container>,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(
|
||||
TypeGroup::decl(),
|
||||
"type TypeGroup = { foo: Array<Container>, };",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Generic::<()>::decl(),
|
||||
"type Generic<T> = { value: T, values: Array<T>, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
GenericAutoBound::<()>::decl(),
|
||||
"type GenericAutoBound<T> = { value: T, values: Array<T>, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
GenericAutoBound2::<()>::decl(),
|
||||
"type GenericAutoBound2<T> = { value: T, values: Array<T>, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Container::decl(),
|
||||
"type Container = { foo: Generic<number>, bar: Array<Generic<number>>, baz: { [key: string]: Generic<string> }, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
enum GenericEnum<A, B, C> {
|
||||
A(A),
|
||||
B(B, B, B),
|
||||
C(Vec<C>),
|
||||
D(Vec<Vec<Vec<A>>>),
|
||||
E { a: A, b: B, c: C },
|
||||
X(Vec<i32>),
|
||||
Y(i32),
|
||||
Z(Vec<Vec<i32>>),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_enum() {
|
||||
assert_eq!(
|
||||
GenericEnum::<(), (), ()>::decl(),
|
||||
r#"type GenericEnum<A, B, C> = { "A": A } | { "B": [B, B, B] } | { "C": Array<C> } | { "D": Array<Array<Array<A>>> } | { "E": { a: A, b: B, c: C, } } | { "X": Array<number> } | { "Y": number } | { "Z": Array<Array<number>> };"#
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct NewType<T>(Vec<Vec<T>>);
|
||||
|
||||
#[test]
|
||||
fn generic_newtype() {
|
||||
assert_eq!(
|
||||
NewType::<()>::decl(),
|
||||
r#"type NewType<T> = Array<Array<T>>;"#
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Tuple<T>(T, Vec<T>, Vec<Vec<T>>);
|
||||
|
||||
#[test]
|
||||
fn generic_tuple() {
|
||||
assert_eq!(
|
||||
Tuple::<()>::decl(),
|
||||
r#"type Tuple<T> = [T, Array<T>, Array<Array<T>>];"#
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Struct<T> {
|
||||
a: T,
|
||||
b: (T, T),
|
||||
c: (T, (T, T)),
|
||||
d: [T; 3],
|
||||
e: [(T, T); 3],
|
||||
f: Vec<T>,
|
||||
g: Vec<Vec<T>>,
|
||||
h: Vec<[(T, T); 3]>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_struct() {
|
||||
assert_eq!(
|
||||
Struct::<()>::decl(),
|
||||
"type Struct<T> = { a: T, b: [T, T], c: [T, [T, T]], d: [T, T, T], e: [[T, T], [T, T], [T, T]], f: Array<T>, g: Array<Array<T>>, h: Array<[[T, T], [T, T], [T, T]]>, };"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericInline<T> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ContainerInline {
|
||||
g: GenericInline<String>,
|
||||
#[ts(inline)]
|
||||
gi: GenericInline<String>,
|
||||
#[ts(flatten)]
|
||||
t: GenericInline<Vec<String>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline() {
|
||||
assert_eq!(
|
||||
GenericInline::<()>::decl(),
|
||||
"type GenericInline<T> = { t: T, };"
|
||||
);
|
||||
assert_eq!(
|
||||
ContainerInline::decl(),
|
||||
"type ContainerInline = { g: GenericInline<string>, gi: { t: string, }, t: Array<string>, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericWithBounds<T: ToString> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ContainerWithBounds {
|
||||
g: GenericWithBounds<String>,
|
||||
|
||||
#[ts(inline)]
|
||||
gi: GenericWithBounds<String>,
|
||||
|
||||
#[ts(flatten)]
|
||||
t: GenericWithBounds<u32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_with_bounds() {
|
||||
assert_eq!(
|
||||
GenericWithBounds::<&'static str>::decl(),
|
||||
"type GenericWithBounds<T> = { t: T, };"
|
||||
);
|
||||
assert_eq!(
|
||||
ContainerWithBounds::decl(),
|
||||
"type ContainerWithBounds = { g: GenericWithBounds<string>, gi: { t: string, }, t: number, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericWithDefault<T = String> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ContainerWithDefault {
|
||||
g: GenericWithDefault<String>,
|
||||
|
||||
#[ts(inline)]
|
||||
gi: GenericWithDefault<String>,
|
||||
|
||||
#[ts(flatten)]
|
||||
t: GenericWithDefault<u32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_with_default() {
|
||||
assert_eq!(
|
||||
GenericWithDefault::<()>::decl(),
|
||||
"type GenericWithDefault<T = string> = { t: T, };"
|
||||
);
|
||||
assert_eq!(
|
||||
ContainerWithDefault::decl(),
|
||||
"type ContainerWithDefault = { g: GenericWithDefault<string>, gi: { t: string, }, t: number, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ADefault<T = String> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct BDefault<U = Option<ADefault<i32>>> {
|
||||
u: U,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct YDefault {
|
||||
a1: ADefault,
|
||||
a2: ADefault<i32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
assert_eq!(
|
||||
ADefault::<()>::decl(),
|
||||
"type ADefault<T = string> = { t: T, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BDefault::<()>::decl(),
|
||||
"type BDefault<U = ADefault<number> | null> = { u: U, };"
|
||||
);
|
||||
assert!(BDefault::<()>::dependencies()
|
||||
.iter()
|
||||
.any(|dep| dep.ts_name == "ADefault"));
|
||||
|
||||
assert_eq!(
|
||||
YDefault::decl(),
|
||||
"type YDefault = { a1: ADefault<string>, a2: ADefault<number>, };"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ATraitBounds<T: ToString = i32> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct BTraitBounds<T: ToString + Debug + Clone + 'static>(T);
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
enum CTraitBounds<T: Copy + Clone + PartialEq, K: Copy + PartialOrd = i32> {
|
||||
A { t: T },
|
||||
B(T),
|
||||
C,
|
||||
D(T, K),
|
||||
}
|
||||
|
||||
// Types with const generics can't be exported
|
||||
#[derive(TS)]
|
||||
struct DTraitBounds<T: ToString, const N: usize> {
|
||||
t: [T; N],
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_bounds() {
|
||||
assert_eq!(
|
||||
ATraitBounds::<i32>::decl(),
|
||||
"type ATraitBounds<T = number> = { t: T, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BTraitBounds::<&'static str>::decl(),
|
||||
"type BTraitBounds<T> = T;"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CTraitBounds::<&'static str, i32>::decl(),
|
||||
r#"type CTraitBounds<T, K = number> = { "A": { t: T, } } | { "B": T } | "C" | { "D": [T, K] };"#
|
||||
);
|
||||
|
||||
let ty = format!(
|
||||
"type DTraitBounds<T> = {{ t: [{}], }};",
|
||||
"T, ".repeat(41).trim_end_matches(", ")
|
||||
);
|
||||
assert_eq!(DTraitBounds::<&str, 41>::decl(), ty)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct T0<T> {
|
||||
t0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct P0<T> {
|
||||
p0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct T1<T> {
|
||||
t0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct P1<T> {
|
||||
p0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Parent {
|
||||
a: T1<T0<u32>>,
|
||||
b: T1<P1<T0<P0<u32>>>>,
|
||||
c: T1<P1<()>>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericParent<T> {
|
||||
a_t: T1<T0<T>>,
|
||||
b_t: T1<P1<T0<P0<T>>>>,
|
||||
c_t: T1<P1<T>>,
|
||||
a_null: T1<T0<()>>,
|
||||
b_null: T1<P1<T0<P0<()>>>>,
|
||||
c_null: T1<P1<()>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deeply_nested() {
|
||||
assert_eq!(
|
||||
Parent::inline(),
|
||||
"{ a: T1<T0<number>>, b: T1<P1<T0<P0<number>>>>, c: T1<P1<null>>, }"
|
||||
);
|
||||
assert_eq!(
|
||||
GenericParent::<()>::decl(),
|
||||
"type GenericParent<T> = { \
|
||||
a_t: T1<T0<T>>, \
|
||||
b_t: T1<P1<T0<P0<T>>>>, \
|
||||
c_t: T1<P1<T>>, \
|
||||
a_null: T1<T0<null>>, \
|
||||
b_null: T1<P1<T0<P0<null>>>>, \
|
||||
c_null: T1<P1<null>>, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct SomeType(String);
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
enum MyEnum<A, B> {
|
||||
VariantA(A),
|
||||
VariantB(B),
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ParentEnum {
|
||||
e: MyEnum<i32, i32>,
|
||||
#[ts(inline)]
|
||||
e1: MyEnum<i32, SomeType>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_generic_enum() {
|
||||
// This fails!
|
||||
// The #[ts(inline)] seems to inline recursively, so not only the definition of `MyEnum`, but
|
||||
// also the definition of `SomeType`.
|
||||
assert_eq!(
|
||||
ParentEnum::decl(),
|
||||
"type ParentEnum = { \
|
||||
e: MyEnum<number, number>, \
|
||||
e1: { \"VariantA\": number } | { \"VariantB\": SomeType }, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
#![allow(clippy::box_collection, clippy::enum_variant_names, dead_code)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::{
|
||||
collections::{BTreeMap, HashSet},
|
||||
fmt::Debug,
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Generic<T>
|
||||
where
|
||||
T: TS,
|
||||
{
|
||||
value: T,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericAutoBound<T> {
|
||||
value: T,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericAutoBound2<T>
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
value: T,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Container {
|
||||
foo: Generic<u32>,
|
||||
bar: Box<HashSet<Generic<u32>>>,
|
||||
baz: Box<BTreeMap<String, Rc<Generic<String>>>>,
|
||||
}
|
||||
|
||||
macro_rules! declare {
|
||||
($(#[$meta:meta])* $name:ident { $($fident:ident: $t:ty),+ $(,)? }) => {
|
||||
$(#[$meta])*
|
||||
struct $name {
|
||||
$(pub $fident: $t),+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare! {
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
TypeGroup {
|
||||
foo: Vec<Container>,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(
|
||||
TypeGroup::decl(),
|
||||
"type TypeGroup = { foo: Array<Container>, };",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Generic::<()>::decl(),
|
||||
"type Generic<T> = { value: T, values: Array<T>, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
GenericAutoBound::<()>::decl(),
|
||||
"type GenericAutoBound<T> = { value: T, values: Array<T>, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
GenericAutoBound2::<()>::decl(),
|
||||
"type GenericAutoBound2<T> = { value: T, values: Array<T>, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Container::decl(),
|
||||
"type Container = { foo: Generic<number>, bar: Array<Generic<number>>, baz: { [key in string]?: Generic<string> }, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
enum GenericEnum<A, B, C> {
|
||||
A(A),
|
||||
B(B, B, B),
|
||||
C(Vec<C>),
|
||||
D(Vec<Vec<Vec<A>>>),
|
||||
E { a: A, b: B, c: C },
|
||||
X(Vec<i32>),
|
||||
Y(i32),
|
||||
Z(Vec<Vec<i32>>),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_enum() {
|
||||
assert_eq!(
|
||||
GenericEnum::<(), (), ()>::decl(),
|
||||
r#"type GenericEnum<A, B, C> = { "A": A } | { "B": [B, B, B] } | { "C": Array<C> } | { "D": Array<Array<Array<A>>> } | { "E": { a: A, b: B, c: C, } } | { "X": Array<number> } | { "Y": number } | { "Z": Array<Array<number>> };"#
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct NewType<T>(Vec<Vec<T>>);
|
||||
|
||||
#[test]
|
||||
fn generic_newtype() {
|
||||
assert_eq!(
|
||||
NewType::<()>::decl(),
|
||||
r#"type NewType<T> = Array<Array<T>>;"#
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Tuple<T>(T, Vec<T>, Vec<Vec<T>>);
|
||||
|
||||
#[test]
|
||||
fn generic_tuple() {
|
||||
assert_eq!(
|
||||
Tuple::<()>::decl(),
|
||||
r#"type Tuple<T> = [T, Array<T>, Array<Array<T>>];"#
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Struct<T> {
|
||||
a: T,
|
||||
b: (T, T),
|
||||
c: (T, (T, T)),
|
||||
d: [T; 3],
|
||||
e: [(T, T); 3],
|
||||
f: Vec<T>,
|
||||
g: Vec<Vec<T>>,
|
||||
h: Vec<[(T, T); 3]>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_struct() {
|
||||
assert_eq!(
|
||||
Struct::<()>::decl(),
|
||||
"type Struct<T> = { a: T, b: [T, T], c: [T, [T, T]], d: [T, T, T], e: [[T, T], [T, T], [T, T]], f: Array<T>, g: Array<Array<T>>, h: Array<[[T, T], [T, T], [T, T]]>, };"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericInline<T> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ContainerInline {
|
||||
g: GenericInline<String>,
|
||||
#[ts(inline)]
|
||||
gi: GenericInline<String>,
|
||||
#[ts(flatten)]
|
||||
t: GenericInline<Vec<String>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline() {
|
||||
assert_eq!(
|
||||
GenericInline::<()>::decl(),
|
||||
"type GenericInline<T> = { t: T, };"
|
||||
);
|
||||
assert_eq!(
|
||||
ContainerInline::decl(),
|
||||
"type ContainerInline = { g: GenericInline<string>, gi: { t: string, }, t: Array<string>, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericWithBounds<T: ToString> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ContainerWithBounds {
|
||||
g: GenericWithBounds<String>,
|
||||
|
||||
#[ts(inline)]
|
||||
gi: GenericWithBounds<String>,
|
||||
|
||||
#[ts(flatten)]
|
||||
t: GenericWithBounds<u32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_with_bounds() {
|
||||
assert_eq!(
|
||||
GenericWithBounds::<&'static str>::decl(),
|
||||
"type GenericWithBounds<T> = { t: T, };"
|
||||
);
|
||||
assert_eq!(
|
||||
ContainerWithBounds::decl(),
|
||||
"type ContainerWithBounds = { g: GenericWithBounds<string>, gi: { t: string, }, t: number, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericWithDefault<T = String> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ContainerWithDefault {
|
||||
g: GenericWithDefault<String>,
|
||||
|
||||
#[ts(inline)]
|
||||
gi: GenericWithDefault<String>,
|
||||
|
||||
#[ts(flatten)]
|
||||
t: GenericWithDefault<u32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_with_default() {
|
||||
assert_eq!(
|
||||
GenericWithDefault::<()>::decl(),
|
||||
"type GenericWithDefault<T = string> = { t: T, };"
|
||||
);
|
||||
assert_eq!(
|
||||
ContainerWithDefault::decl(),
|
||||
"type ContainerWithDefault = { g: GenericWithDefault<string>, gi: { t: string, }, t: number, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ADefault<T = String> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct BDefault<U = Option<ADefault<i32>>> {
|
||||
u: U,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct YDefault {
|
||||
a1: ADefault,
|
||||
a2: ADefault<i32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
assert_eq!(
|
||||
ADefault::<()>::decl(),
|
||||
"type ADefault<T = string> = { t: T, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BDefault::<()>::decl(),
|
||||
"type BDefault<U = ADefault<number> | null> = { u: U, };"
|
||||
);
|
||||
assert!(BDefault::<()>::dependencies()
|
||||
.iter()
|
||||
.any(|dep| dep.ts_name == "ADefault"));
|
||||
|
||||
assert_eq!(
|
||||
YDefault::decl(),
|
||||
"type YDefault = { a1: ADefault<string>, a2: ADefault<number>, };"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ATraitBounds<T: ToString = i32> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct BTraitBounds<T: ToString + Debug + Clone + 'static>(T);
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
enum CTraitBounds<T: Copy + Clone + PartialEq, K: Copy + PartialOrd = i32> {
|
||||
A { t: T },
|
||||
B(T),
|
||||
C,
|
||||
D(T, K),
|
||||
}
|
||||
|
||||
// Types with const generics can't be exported
|
||||
#[derive(TS)]
|
||||
struct DTraitBounds<T: ToString, const N: usize> {
|
||||
t: [T; N],
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_bounds() {
|
||||
assert_eq!(
|
||||
ATraitBounds::<i32>::decl(),
|
||||
"type ATraitBounds<T = number> = { t: T, };"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BTraitBounds::<&'static str>::decl(),
|
||||
"type BTraitBounds<T> = T;"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
CTraitBounds::<&'static str, i32>::decl(),
|
||||
r#"type CTraitBounds<T, K = number> = { "A": { t: T, } } | { "B": T } | "C" | { "D": [T, K] };"#
|
||||
);
|
||||
|
||||
let ty = format!(
|
||||
"type DTraitBounds<T> = {{ t: [{}], }};",
|
||||
"T, ".repeat(41).trim_end_matches(", ")
|
||||
);
|
||||
assert_eq!(DTraitBounds::<&str, 41>::decl(), ty)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct T0<T> {
|
||||
t0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct P0<T> {
|
||||
p0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct T1<T> {
|
||||
t0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct P1<T> {
|
||||
p0: T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct Parent {
|
||||
a: T1<T0<u32>>,
|
||||
b: T1<P1<T0<P0<u32>>>>,
|
||||
c: T1<P1<()>>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct GenericParent<T> {
|
||||
a_t: T1<T0<T>>,
|
||||
b_t: T1<P1<T0<P0<T>>>>,
|
||||
c_t: T1<P1<T>>,
|
||||
a_null: T1<T0<()>>,
|
||||
b_null: T1<P1<T0<P0<()>>>>,
|
||||
c_null: T1<P1<()>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deeply_nested() {
|
||||
assert_eq!(
|
||||
Parent::inline(),
|
||||
"{ a: T1<T0<number>>, b: T1<P1<T0<P0<number>>>>, c: T1<P1<null>>, }"
|
||||
);
|
||||
assert_eq!(
|
||||
GenericParent::<()>::decl(),
|
||||
"type GenericParent<T> = { \
|
||||
a_t: T1<T0<T>>, \
|
||||
b_t: T1<P1<T0<P0<T>>>>, \
|
||||
c_t: T1<P1<T>>, \
|
||||
a_null: T1<T0<null>>, \
|
||||
b_null: T1<P1<T0<P0<null>>>>, \
|
||||
c_null: T1<P1<null>>, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct SomeType(String);
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
enum MyEnum<A, B> {
|
||||
VariantA(A),
|
||||
VariantB(B),
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "generics/")]
|
||||
struct ParentEnum {
|
||||
e: MyEnum<i32, i32>,
|
||||
#[ts(inline)]
|
||||
e1: MyEnum<i32, SomeType>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_generic_enum() {
|
||||
// This fails!
|
||||
// The #[ts(inline)] seems to inline recursively, so not only the definition of `MyEnum`, but
|
||||
// also the definition of `SomeType`.
|
||||
assert_eq!(
|
||||
ParentEnum::decl(),
|
||||
"type ParentEnum = { \
|
||||
e: MyEnum<number, number>, \
|
||||
e1: { \"VariantA\": number } | { \"VariantB\": SomeType }, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,72 +1,72 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct Hashes {
|
||||
map: HashMap<String, String>,
|
||||
set: HashSet<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hashmap() {
|
||||
assert_eq!(
|
||||
Hashes::decl(),
|
||||
"type Hashes = { map: { [key: string]: string }, set: Array<string>, };"
|
||||
)
|
||||
}
|
||||
|
||||
struct CustomHasher {}
|
||||
|
||||
type CustomHashMap<K, V> = HashMap<K, V, CustomHasher>;
|
||||
type CustomHashSet<K> = HashSet<K, CustomHasher>;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct HashesHasher {
|
||||
map: CustomHashMap<String, String>,
|
||||
set: CustomHashSet<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hashmap_with_custom_hasher() {
|
||||
assert_eq!(
|
||||
HashesHasher::decl(),
|
||||
"type HashesHasher = { map: { [key: string]: string }, set: Array<string>, };"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS, Eq, PartialEq, Hash)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct CustomKey(String);
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct CustomValue;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct HashMapWithCustomTypes {
|
||||
map: HashMap<CustomKey, CustomValue>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct BTreeMapWithCustomTypes {
|
||||
map: BTreeMap<CustomKey, CustomValue>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_custom_types() {
|
||||
assert_eq!(
|
||||
HashMapWithCustomTypes::inline(),
|
||||
BTreeMapWithCustomTypes::inline()
|
||||
);
|
||||
assert_eq!(
|
||||
HashMapWithCustomTypes::decl(),
|
||||
"type HashMapWithCustomTypes = { map: { [key: CustomKey]: CustomValue }, };"
|
||||
);
|
||||
}
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct Hashes {
|
||||
map: HashMap<String, String>,
|
||||
set: HashSet<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hashmap() {
|
||||
assert_eq!(
|
||||
Hashes::decl(),
|
||||
"type Hashes = { map: { [key in string]?: string }, set: Array<string>, };"
|
||||
)
|
||||
}
|
||||
|
||||
struct CustomHasher {}
|
||||
|
||||
type CustomHashMap<K, V> = HashMap<K, V, CustomHasher>;
|
||||
type CustomHashSet<K> = HashSet<K, CustomHasher>;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct HashesHasher {
|
||||
map: CustomHashMap<String, String>,
|
||||
set: CustomHashSet<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hashmap_with_custom_hasher() {
|
||||
assert_eq!(
|
||||
HashesHasher::decl(),
|
||||
"type HashesHasher = { map: { [key in string]?: string }, set: Array<string>, };"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS, Eq, PartialEq, Hash)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct CustomKey(String);
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct CustomValue;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct HashMapWithCustomTypes {
|
||||
map: HashMap<CustomKey, CustomValue>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "hashmap/")]
|
||||
struct BTreeMapWithCustomTypes {
|
||||
map: BTreeMap<CustomKey, CustomValue>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_custom_types() {
|
||||
assert_eq!(
|
||||
HashMapWithCustomTypes::inline(),
|
||||
BTreeMapWithCustomTypes::inline()
|
||||
);
|
||||
assert_eq!(
|
||||
HashMapWithCustomTypes::decl(),
|
||||
"type HashMapWithCustomTypes = { map: { [key in CustomKey]?: CustomValue }, };"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
#![allow(dead_code)]
|
||||
#![cfg(feature = "indexmap-impl")]
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "indexmap/")]
|
||||
struct Indexes {
|
||||
map: IndexMap<String, String>,
|
||||
set: IndexSet<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexmap() {
|
||||
assert_eq!(
|
||||
Indexes::decl(),
|
||||
"type Indexes = { map: { [key: string]: string }, set: Array<string>, };"
|
||||
)
|
||||
}
|
||||
#![allow(dead_code)]
|
||||
#![cfg(feature = "indexmap-impl")]
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "indexmap/")]
|
||||
struct Indexes {
|
||||
map: IndexMap<String, String>,
|
||||
set: IndexSet<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexmap() {
|
||||
assert_eq!(
|
||||
Indexes::decl(),
|
||||
"type Indexes = { map: { [key in string]?: string }, set: Array<string>, };"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -38,13 +38,13 @@ fn issue_168() {
|
||||
FooInlined::export_to_string().unwrap(),
|
||||
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\
|
||||
\n\
|
||||
export type FooInlined = { map: { [key: number]: { map: { [key: number]: { map: { [key: number]: string }, } }, } }, };\n"
|
||||
export type FooInlined = { map: { [key in number]?: { map: { [key in number]?: { map: { [key in number]?: string }, } }, } }, };\n"
|
||||
);
|
||||
assert_eq!(
|
||||
Foo::export_to_string().unwrap(),
|
||||
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\
|
||||
import type { Bar } from \"./Bar\";\n\
|
||||
\n\
|
||||
export type Foo = { map: { [key: number]: Bar }, };\n"
|
||||
export type Foo = { map: { [key in number]?: Bar }, };\n"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
use std::collections::HashMap;
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_338/")]
|
||||
pub struct MyType {
|
||||
pub my_field_0: bool,
|
||||
pub my_field_1: HashMap<MyEnum, MyStruct>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_338/")]
|
||||
pub enum MyEnum {
|
||||
Variant0,
|
||||
Variant1,
|
||||
Variant2,
|
||||
Variant3,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_338/")]
|
||||
pub struct MyStruct {
|
||||
pub my_field_0: bool,
|
||||
pub my_field_1: u32,
|
||||
pub my_field_2: Option<u32>,
|
||||
pub my_field_3: Option<u32>,
|
||||
pub my_field_4: Option<u32>,
|
||||
pub my_field_5: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(
|
||||
MyType::inline(),
|
||||
"{ my_field_0: boolean, my_field_1: { [key in MyEnum]?: MyStruct }, }"
|
||||
);
|
||||
}
|
||||
@@ -1,77 +1,77 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
type TypeAlias = HashMap<String, String>;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
enum Enum {
|
||||
A(TypeAlias),
|
||||
B(HashMap<String, String>),
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct Struct {
|
||||
a: TypeAlias,
|
||||
b: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_70() {
|
||||
assert_eq!(
|
||||
Enum::decl(),
|
||||
"type Enum = { \"A\": { [key: string]: string } } | { \"B\": { [key: string]: string } };"
|
||||
);
|
||||
assert_eq!(
|
||||
Struct::decl(),
|
||||
"type Struct = { a: { [key: string]: string }, b: { [key: string]: string }, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct GenericType<T, U> {
|
||||
foo: T,
|
||||
bar: U,
|
||||
}
|
||||
|
||||
type GenericAlias<A = String, B = String> = GenericType<(A, String), Vec<(B, i32)>>;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct Container {
|
||||
a: GenericAlias<Vec<i32>, Vec<String>>,
|
||||
b: GenericAlias,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct GenericContainer<A, B = i32> {
|
||||
a: GenericAlias,
|
||||
b: GenericAlias<A, B>,
|
||||
c: GenericAlias<A, GenericAlias<A, B>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic() {
|
||||
assert_eq!(
|
||||
Container::decl(),
|
||||
"type Container = { \
|
||||
a: GenericType<[Array<number>, string], Array<[Array<string>, number]>>, \
|
||||
b: GenericType<[string, string], Array<[string, number]>>, \
|
||||
};"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
GenericContainer::<(), ()>::decl(),
|
||||
"type GenericContainer<A, B = number> = { \
|
||||
a: GenericType<[string, string], Array<[string, number]>>, \
|
||||
b: GenericType<[A, string], Array<[B, number]>>, \
|
||||
c: GenericType<[A, string], Array<[GenericType<[A, string], Array<[B, number]>>, number]>>, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
#![allow(unused)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
type TypeAlias = HashMap<String, String>;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
enum Enum {
|
||||
A(TypeAlias),
|
||||
B(HashMap<String, String>),
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct Struct {
|
||||
a: TypeAlias,
|
||||
b: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_70() {
|
||||
assert_eq!(
|
||||
Enum::decl(),
|
||||
"type Enum = { \"A\": { [key in string]?: string } } | { \"B\": { [key in string]?: string } };"
|
||||
);
|
||||
assert_eq!(
|
||||
Struct::decl(),
|
||||
"type Struct = { a: { [key in string]?: string }, b: { [key in string]?: string }, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct GenericType<T, U> {
|
||||
foo: T,
|
||||
bar: U,
|
||||
}
|
||||
|
||||
type GenericAlias<A = String, B = String> = GenericType<(A, String), Vec<(B, i32)>>;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct Container {
|
||||
a: GenericAlias<Vec<i32>, Vec<String>>,
|
||||
b: GenericAlias,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "issue_70/")]
|
||||
struct GenericContainer<A, B = i32> {
|
||||
a: GenericAlias,
|
||||
b: GenericAlias<A, B>,
|
||||
c: GenericAlias<A, GenericAlias<A, B>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic() {
|
||||
assert_eq!(
|
||||
Container::decl(),
|
||||
"type Container = { \
|
||||
a: GenericType<[Array<number>, string], Array<[Array<string>, number]>>, \
|
||||
b: GenericType<[string, string], Array<[string, number]>>, \
|
||||
};"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
GenericContainer::<(), ()>::decl(),
|
||||
"type GenericContainer<A, B = number> = { \
|
||||
a: GenericType<[string, string], Array<[string, number]>>, \
|
||||
b: GenericType<[A, string], Array<[B, number]>>, \
|
||||
c: GenericType<[A, string], Array<[GenericType<[A, string], Array<[B, number]>>, number]>>, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "lifetimes/")]
|
||||
struct S<'a> {
|
||||
s: &'a str,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "lifetimes/")]
|
||||
struct B<'a, T: 'a> {
|
||||
a: &'a T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "lifetimes/")]
|
||||
struct A<'a> {
|
||||
a: &'a &'a &'a Vec<u32>, //Multiple References
|
||||
b: &'a Vec<B<'a, u32>>, //Nesting
|
||||
c: &'a std::collections::HashMap<String, bool>, //Multiple type args
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_borrow() {
|
||||
assert_eq!(S::decl(), "type S = { s: string, };")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_borrow_type_args() {
|
||||
assert_eq!(
|
||||
A::decl(),
|
||||
"type A = { a: Array<number>, b: Array<B<number>>, c: { [key: string]: boolean }, };"
|
||||
);
|
||||
}
|
||||
#![allow(dead_code)]
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "lifetimes/")]
|
||||
struct S<'a> {
|
||||
s: &'a str,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "lifetimes/")]
|
||||
struct B<'a, T: 'a> {
|
||||
a: &'a T,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "lifetimes/")]
|
||||
struct A<'a> {
|
||||
a: &'a &'a &'a Vec<u32>, //Multiple References
|
||||
b: &'a Vec<B<'a, u32>>, //Nesting
|
||||
c: &'a std::collections::HashMap<String, bool>, //Multiple type args
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_borrow() {
|
||||
assert_eq!(S::decl(), "type S = { s: string, };")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_borrow_type_args() {
|
||||
assert_eq!(
|
||||
A::decl(),
|
||||
"type A = { a: Array<number>, b: Array<B<number>>, c: { [key in string]?: boolean }, };"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ mod issue_168;
|
||||
mod issue_232;
|
||||
mod issue_308;
|
||||
mod issue_317;
|
||||
mod issue_338;
|
||||
mod issue_70;
|
||||
mod issue_80;
|
||||
mod leading_colon;
|
||||
|
||||
@@ -1,188 +1,188 @@
|
||||
#![allow(dead_code)]
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
#[cfg(feature = "serde-compat")]
|
||||
use serde::Serialize;
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "self_referential/")]
|
||||
struct HasT {
|
||||
t: &'static T<'static>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "self_referential/")]
|
||||
struct T<'a> {
|
||||
t_box: Box<T<'a>>,
|
||||
self_box: Box<Self>,
|
||||
|
||||
t_ref: &'a T<'a>,
|
||||
self_ref: &'a Self,
|
||||
|
||||
t_arc: Arc<T<'a>>,
|
||||
self_arc: Arc<Self>,
|
||||
|
||||
#[ts(inline)]
|
||||
has_t: HasT,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn named() {
|
||||
assert_eq!(
|
||||
T::decl(),
|
||||
"type T = { \
|
||||
t_box: T, \
|
||||
self_box: T, \
|
||||
t_ref: T, \
|
||||
self_ref: T, \
|
||||
t_arc: T, \
|
||||
self_arc: T, \
|
||||
has_t: { t: T, }, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "self_referential/", rename = "E")]
|
||||
enum ExternallyTagged {
|
||||
A(Box<ExternallyTagged>),
|
||||
B(&'static ExternallyTagged),
|
||||
C(Box<Self>),
|
||||
D(&'static Self),
|
||||
E(
|
||||
Box<ExternallyTagged>,
|
||||
Box<Self>,
|
||||
&'static ExternallyTagged,
|
||||
&'static Self,
|
||||
),
|
||||
F {
|
||||
a: Box<Self>,
|
||||
b: &'static ExternallyTagged,
|
||||
c: HashMap<String, ExternallyTagged>,
|
||||
d: Option<Arc<ExternallyTagged>>,
|
||||
#[ts(optional = nullable)]
|
||||
e: Option<Arc<ExternallyTagged>>,
|
||||
#[ts(optional)]
|
||||
f: Option<Arc<ExternallyTagged>>,
|
||||
},
|
||||
|
||||
G(
|
||||
Vec<ExternallyTagged>,
|
||||
[&'static ExternallyTagged; 1024],
|
||||
HashMap<String, ExternallyTagged>,
|
||||
),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_externally_tagged() {
|
||||
assert_eq!(
|
||||
ExternallyTagged::decl(),
|
||||
"type E = { \"A\": E } | \
|
||||
{ \"B\": E } | \
|
||||
{ \"C\": E } | \
|
||||
{ \"D\": E } | \
|
||||
{ \"E\": [E, E, E, E] } | \
|
||||
{ \"F\": { a: E, b: E, c: { [key: string]: E }, d: E | null, e?: E | null, f?: E, } } | \
|
||||
{ \"G\": [Array<E>, Array<E>, { [key: string]: E }] };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
|
||||
#[ts(rename = "I")]
|
||||
#[cfg_attr(feature = "serde-compat", serde(tag = "tag"))]
|
||||
#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag"))]
|
||||
enum InternallyTagged {
|
||||
A(Box<InternallyTagged>),
|
||||
B(&'static InternallyTagged),
|
||||
C(Box<Self>),
|
||||
D(&'static Self),
|
||||
E(Vec<Self>),
|
||||
F {
|
||||
a: Box<Self>,
|
||||
b: &'static InternallyTagged,
|
||||
c: HashMap<InternallyTagged, InternallyTagged>,
|
||||
d: Option<&'static InternallyTagged>,
|
||||
#[ts(optional = nullable)]
|
||||
e: Option<&'static InternallyTagged>,
|
||||
#[ts(optional)]
|
||||
f: Option<&'static InternallyTagged>,
|
||||
},
|
||||
}
|
||||
|
||||
// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types
|
||||
// gets lost during the translation to TypeScript (e.g "Box<T>" => "T").
|
||||
#[test]
|
||||
fn enum_internally_tagged() {
|
||||
assert_eq!(
|
||||
InternallyTagged::decl(),
|
||||
"type I = { \"tag\": \"A\" } & I | \
|
||||
{ \"tag\": \"B\" } & I | \
|
||||
{ \"tag\": \"C\" } & I | \
|
||||
{ \"tag\": \"D\" } & I | \
|
||||
{ \"tag\": \"E\" } & Array<I> | \
|
||||
{ \"tag\": \"F\", a: I, b: I, c: { [key: I]: I }, d: I | null, e?: I | null, f?: I, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
|
||||
#[ts(export, export_to = "self_referential/", rename = "A")]
|
||||
#[cfg_attr(feature = "serde-compat", serde(tag = "tag", content = "content"))]
|
||||
#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag", content = "content"))]
|
||||
enum AdjacentlyTagged {
|
||||
A(Box<AdjacentlyTagged>),
|
||||
B(&'static AdjacentlyTagged),
|
||||
C(Box<Self>),
|
||||
D(&'static Self),
|
||||
E(Vec<Self>),
|
||||
F {
|
||||
a: Box<Self>,
|
||||
b: &'static AdjacentlyTagged,
|
||||
c: HashMap<String, AdjacentlyTagged>,
|
||||
d: Option<&'static AdjacentlyTagged>,
|
||||
#[ts(optional = nullable)]
|
||||
e: Option<&'static AdjacentlyTagged>,
|
||||
#[ts(optional)]
|
||||
f: Option<&'static AdjacentlyTagged>,
|
||||
},
|
||||
G(
|
||||
Vec<Self>,
|
||||
[&'static AdjacentlyTagged; 4],
|
||||
HashMap<String, AdjacentlyTagged>,
|
||||
),
|
||||
}
|
||||
|
||||
// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types
|
||||
// gets lost during the translation to TypeScript (e.g "Box<T>" => "T").
|
||||
#[test]
|
||||
fn enum_adjacently_tagged() {
|
||||
assert_eq!(
|
||||
AdjacentlyTagged::decl(),
|
||||
"type A = { \"tag\": \"A\", \"content\": A } | \
|
||||
{ \"tag\": \"B\", \"content\": A } | \
|
||||
{ \"tag\": \"C\", \"content\": A } | \
|
||||
{ \"tag\": \"D\", \"content\": A } | \
|
||||
{ \"tag\": \"E\", \"content\": Array<A> } | \
|
||||
{ \
|
||||
\"tag\": \"F\", \
|
||||
\"content\": { \
|
||||
a: A, \
|
||||
b: A, \
|
||||
c: { [key: string]: A }, \
|
||||
d: A | null, \
|
||||
e?: A | null, \
|
||||
f?: A, \
|
||||
} \
|
||||
} | \
|
||||
{ \
|
||||
\"tag\": \"G\", \
|
||||
\"content\": [\
|
||||
Array<A>, \
|
||||
[A, A, A, A], \
|
||||
{ [key: string]: A }\
|
||||
] \
|
||||
};"
|
||||
);
|
||||
}
|
||||
#![allow(dead_code)]
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
#[cfg(feature = "serde-compat")]
|
||||
use serde::Serialize;
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "self_referential/")]
|
||||
struct HasT {
|
||||
t: &'static T<'static>,
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "self_referential/")]
|
||||
struct T<'a> {
|
||||
t_box: Box<T<'a>>,
|
||||
self_box: Box<Self>,
|
||||
|
||||
t_ref: &'a T<'a>,
|
||||
self_ref: &'a Self,
|
||||
|
||||
t_arc: Arc<T<'a>>,
|
||||
self_arc: Arc<Self>,
|
||||
|
||||
#[ts(inline)]
|
||||
has_t: HasT,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn named() {
|
||||
assert_eq!(
|
||||
T::decl(),
|
||||
"type T = { \
|
||||
t_box: T, \
|
||||
self_box: T, \
|
||||
t_ref: T, \
|
||||
self_ref: T, \
|
||||
t_arc: T, \
|
||||
self_arc: T, \
|
||||
has_t: { t: T, }, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "self_referential/", rename = "E")]
|
||||
enum ExternallyTagged {
|
||||
A(Box<ExternallyTagged>),
|
||||
B(&'static ExternallyTagged),
|
||||
C(Box<Self>),
|
||||
D(&'static Self),
|
||||
E(
|
||||
Box<ExternallyTagged>,
|
||||
Box<Self>,
|
||||
&'static ExternallyTagged,
|
||||
&'static Self,
|
||||
),
|
||||
F {
|
||||
a: Box<Self>,
|
||||
b: &'static ExternallyTagged,
|
||||
c: HashMap<String, ExternallyTagged>,
|
||||
d: Option<Arc<ExternallyTagged>>,
|
||||
#[ts(optional = nullable)]
|
||||
e: Option<Arc<ExternallyTagged>>,
|
||||
#[ts(optional)]
|
||||
f: Option<Arc<ExternallyTagged>>,
|
||||
},
|
||||
|
||||
G(
|
||||
Vec<ExternallyTagged>,
|
||||
[&'static ExternallyTagged; 1024],
|
||||
HashMap<String, ExternallyTagged>,
|
||||
),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_externally_tagged() {
|
||||
assert_eq!(
|
||||
ExternallyTagged::decl(),
|
||||
"type E = { \"A\": E } | \
|
||||
{ \"B\": E } | \
|
||||
{ \"C\": E } | \
|
||||
{ \"D\": E } | \
|
||||
{ \"E\": [E, E, E, E] } | \
|
||||
{ \"F\": { a: E, b: E, c: { [key in string]?: E }, d: E | null, e?: E | null, f?: E, } } | \
|
||||
{ \"G\": [Array<E>, Array<E>, { [key in string]?: E }] };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
|
||||
#[ts(rename = "I")]
|
||||
#[cfg_attr(feature = "serde-compat", serde(tag = "tag"))]
|
||||
#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag"))]
|
||||
enum InternallyTagged {
|
||||
A(Box<InternallyTagged>),
|
||||
B(&'static InternallyTagged),
|
||||
C(Box<Self>),
|
||||
D(&'static Self),
|
||||
E(Vec<Self>),
|
||||
F {
|
||||
a: Box<Self>,
|
||||
b: &'static InternallyTagged,
|
||||
c: HashMap<InternallyTagged, InternallyTagged>,
|
||||
d: Option<&'static InternallyTagged>,
|
||||
#[ts(optional = nullable)]
|
||||
e: Option<&'static InternallyTagged>,
|
||||
#[ts(optional)]
|
||||
f: Option<&'static InternallyTagged>,
|
||||
},
|
||||
}
|
||||
|
||||
// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types
|
||||
// gets lost during the translation to TypeScript (e.g "Box<T>" => "T").
|
||||
#[test]
|
||||
fn enum_internally_tagged() {
|
||||
assert_eq!(
|
||||
InternallyTagged::decl(),
|
||||
"type I = { \"tag\": \"A\" } & I | \
|
||||
{ \"tag\": \"B\" } & I | \
|
||||
{ \"tag\": \"C\" } & I | \
|
||||
{ \"tag\": \"D\" } & I | \
|
||||
{ \"tag\": \"E\" } & Array<I> | \
|
||||
{ \"tag\": \"F\", a: I, b: I, c: { [key in I]?: I }, d: I | null, e?: I | null, f?: I, };"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
|
||||
#[ts(export, export_to = "self_referential/", rename = "A")]
|
||||
#[cfg_attr(feature = "serde-compat", serde(tag = "tag", content = "content"))]
|
||||
#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag", content = "content"))]
|
||||
enum AdjacentlyTagged {
|
||||
A(Box<AdjacentlyTagged>),
|
||||
B(&'static AdjacentlyTagged),
|
||||
C(Box<Self>),
|
||||
D(&'static Self),
|
||||
E(Vec<Self>),
|
||||
F {
|
||||
a: Box<Self>,
|
||||
b: &'static AdjacentlyTagged,
|
||||
c: HashMap<String, AdjacentlyTagged>,
|
||||
d: Option<&'static AdjacentlyTagged>,
|
||||
#[ts(optional = nullable)]
|
||||
e: Option<&'static AdjacentlyTagged>,
|
||||
#[ts(optional)]
|
||||
f: Option<&'static AdjacentlyTagged>,
|
||||
},
|
||||
G(
|
||||
Vec<Self>,
|
||||
[&'static AdjacentlyTagged; 4],
|
||||
HashMap<String, AdjacentlyTagged>,
|
||||
),
|
||||
}
|
||||
|
||||
// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types
|
||||
// gets lost during the translation to TypeScript (e.g "Box<T>" => "T").
|
||||
#[test]
|
||||
fn enum_adjacently_tagged() {
|
||||
assert_eq!(
|
||||
AdjacentlyTagged::decl(),
|
||||
"type A = { \"tag\": \"A\", \"content\": A } | \
|
||||
{ \"tag\": \"B\", \"content\": A } | \
|
||||
{ \"tag\": \"C\", \"content\": A } | \
|
||||
{ \"tag\": \"D\", \"content\": A } | \
|
||||
{ \"tag\": \"E\", \"content\": Array<A> } | \
|
||||
{ \
|
||||
\"tag\": \"F\", \
|
||||
\"content\": { \
|
||||
a: A, \
|
||||
b: A, \
|
||||
c: { [key in string]?: A }, \
|
||||
d: A | null, \
|
||||
e?: A | null, \
|
||||
f?: A, \
|
||||
} \
|
||||
} | \
|
||||
{ \
|
||||
\"tag\": \"G\", \
|
||||
\"content\": [\
|
||||
Array<A>, \
|
||||
[A, A, A, A], \
|
||||
{ [key in string]?: A }\
|
||||
] \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,65 +1,65 @@
|
||||
#![cfg(feature = "serde_json")]
|
||||
#![allow(unused)]
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "serde_json_impl/")]
|
||||
struct UsingSerdeJson {
|
||||
num: serde_json::Number,
|
||||
map1: serde_json::Map<String, i32>,
|
||||
map2: serde_json::Map<String, UsingSerdeJson>,
|
||||
map3: serde_json::Map<String, serde_json::Map<String, i32>>,
|
||||
map4: serde_json::Map<String, serde_json::Number>,
|
||||
map5: serde_json::Map<String, serde_json::Value>,
|
||||
any: serde_json::Value,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn using_serde_json() {
|
||||
assert_eq!(serde_json::Number::inline(), "number");
|
||||
assert_eq!(
|
||||
serde_json::Map::<String, i32>::inline(),
|
||||
"{ [key: string]: number }"
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::Value::decl(),
|
||||
"type JsonValue = number | string | Array<JsonValue> | { [key: string]: JsonValue };",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
UsingSerdeJson::decl(),
|
||||
"type UsingSerdeJson = { \
|
||||
num: number, \
|
||||
map1: { [key: string]: number }, \
|
||||
map2: { [key: string]: UsingSerdeJson }, \
|
||||
map3: { [key: string]: { [key: string]: number } }, \
|
||||
map4: { [key: string]: number }, \
|
||||
map5: { [key: string]: JsonValue }, \
|
||||
any: JsonValue, \
|
||||
};"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "serde_json_impl/")]
|
||||
struct InlinedValue {
|
||||
#[ts(inline)]
|
||||
any: serde_json::Value,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inlined_value() {
|
||||
assert_eq!(
|
||||
InlinedValue::decl(),
|
||||
"type InlinedValue = { \
|
||||
any: number | string | Array<JsonValue> | { [key: string]: JsonValue }, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "serde_json_impl/")]
|
||||
struct Simple {
|
||||
json: serde_json::Value,
|
||||
}
|
||||
#![cfg(feature = "serde_json")]
|
||||
#![allow(unused)]
|
||||
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "serde_json_impl/")]
|
||||
struct UsingSerdeJson {
|
||||
num: serde_json::Number,
|
||||
map1: serde_json::Map<String, i32>,
|
||||
map2: serde_json::Map<String, UsingSerdeJson>,
|
||||
map3: serde_json::Map<String, serde_json::Map<String, i32>>,
|
||||
map4: serde_json::Map<String, serde_json::Number>,
|
||||
map5: serde_json::Map<String, serde_json::Value>,
|
||||
any: serde_json::Value,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn using_serde_json() {
|
||||
assert_eq!(serde_json::Number::inline(), "number");
|
||||
assert_eq!(
|
||||
serde_json::Map::<String, i32>::inline(),
|
||||
"{ [key in string]?: number }"
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::Value::decl(),
|
||||
"type JsonValue = number | string | Array<JsonValue> | { [key in string]?: JsonValue };",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
UsingSerdeJson::decl(),
|
||||
"type UsingSerdeJson = { \
|
||||
num: number, \
|
||||
map1: { [key in string]?: number }, \
|
||||
map2: { [key in string]?: UsingSerdeJson }, \
|
||||
map3: { [key in string]?: { [key in string]?: number } }, \
|
||||
map4: { [key in string]?: number }, \
|
||||
map5: { [key in string]?: JsonValue }, \
|
||||
any: JsonValue, \
|
||||
};"
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "serde_json_impl/")]
|
||||
struct InlinedValue {
|
||||
#[ts(inline)]
|
||||
any: serde_json::Value,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inlined_value() {
|
||||
assert_eq!(
|
||||
InlinedValue::decl(),
|
||||
"type InlinedValue = { \
|
||||
any: number | string | Array<JsonValue> | { [key in string]?: JsonValue }, \
|
||||
};"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(TS)]
|
||||
#[ts(export, export_to = "serde_json_impl/")]
|
||||
struct Simple {
|
||||
json: serde_json::Value,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user