Skip to content

Commit

Permalink
fixes #156
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Oct 16, 2023
1 parent 4dea4d5 commit 034a9c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lang/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ pub(crate) fn datatype_inner(ctx: ExportContext, typ: &DataType, type_map: &Type
// We use `T[]` instead of `Array<T>` to avoid issues with circular references.
DataType::List(def) => {
let dt = datatype_inner(ctx, def, type_map)?;
if dt.contains(' ') && !dt.ends_with('}') {
if (dt.contains(' ') && !dt.ends_with('}'))
// This is to do with maintaining order of operations.
// Eg `{} | {}` must be wrapped in parens like `({} | {})[]` but `{}` doesn't cause `{}[]` is valid
|| (dt.contains(' ') && (dt.contains("&") || dt.contains("|")))
{
format!("({dt})[]")
} else {
format!("{dt}[]")
Expand Down
11 changes: 11 additions & 0 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub(crate) use assert_ts_export;

#[test]
fn typescript_types() {
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);

assert_ts!(i8, "number");
assert_ts!(u8, "number");
assert_ts!(i16, "number");
Expand Down Expand Up @@ -272,6 +274,9 @@ fn typescript_types() {
RenameWithWeirdCharsEnum,
ExportError::InvalidName(NamedLocation::Type, ExportPath::new_unsafe("@odata.context"), r#"@odata.context"#.to_string())
);

// https://github.com/oscartbeaumont/specta/issues/156
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);
}

#[derive(Type)]
Expand Down Expand Up @@ -595,3 +600,9 @@ pub struct RenameWithWeirdCharsStruct(String);
#[derive(Type)]
#[specta(export = false, rename = "@odata.context")]
pub enum RenameWithWeirdCharsEnum {}

#[derive(Serialize, Type)]
pub enum MyEnum {
A(String),
B(u32),
}

0 comments on commit 034a9c0

Please sign in to comment.