Skip to content

Commit

Permalink
fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 24, 2023
1 parent 7b970f1 commit 78c07c7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/lang/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ fn datatype_inner(ctx: ExportContext, typ: &DataType) -> Result<String, TsExport
)
}
// We use `T[]` instead of `Array<T>` to avoid issues with circular references.
DataType::List(def) => format!("{}[]", datatype_inner(ctx, def)?),
DataType::List(def) => {
let dt = datatype_inner(ctx, def)?;
if dt.contains(' ') {
format!("({dt})[]")
} else {
format!("{dt}[]")
}
}
DataType::Named(NamedDataType {
name,
item: NamedDataTypeItem::Tuple(TupleType { fields, .. }),
Expand Down
3 changes: 3 additions & 0 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ fn typescript_types() {
// assert_ts!(() => ..5, r#"{ end: 5 }"#);
// assert_ts!(() => ..=5, r#"{ end: 5 }"#);

// https://github.com/oscartbeaumont/specta/issues/66
assert_ts!([Option<u8>; 16], r#"(number | null)[]"#);

// assert_ts_export!(DeprecatedType, "");
// assert_ts_export!(DeprecatedTypeWithMsg, "");
// assert_ts_export!(DeprecatedFields, "");
Expand Down
2 changes: 1 addition & 1 deletion tests/ts_rs/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn generic_struct() {

assert_ts_export!(
GenericStruct2::<()>,
"export type GenericStruct2<T> = { a: T; b: [T, T]; c: [T, [T, T]]; d: T[]; e: [T, T][]; f: T[]; g: T[][]; h: [T, T][][] }"
"export type GenericStruct2<T> = { a: T; b: [T, T]; c: [T, [T, T]]; d: T[]; e: ([T, T])[]; f: T[]; g: T[][]; h: (([T, T])[])[] }"
)
}

Expand Down

0 comments on commit 78c07c7

Please sign in to comment.