Skip to content

Commit

Permalink
fixes #60 - nesting of options
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 24, 2023
1 parent 32d7de4 commit 216cf15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lang/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ fn datatype_inner(ctx: ExportContext, typ: &DataType) -> Result<String, TsExport
}
}
DataType::Literal(literal) => literal.to_ts(),
DataType::Nullable(def) => format!("{} | null", datatype_inner(ctx, def)?),
DataType::Nullable(def) => {
let dt = datatype_inner(ctx, def)?;

if dt.ends_with(" | null") {
dt
} else {
format!("{dt} | null",)
}
}
DataType::Record(def) => {
let divider = match &def.0 {
DataType::Enum(_) => " in",
Expand Down
3 changes: 3 additions & 0 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ fn typescript_types() {
// https://github.com/oscartbeaumont/specta/issues/65
assert_ts!(HashMap<BasicEnum, ()>, r#"{ [key in "A" | "B"]: null }"#);

// https://github.com/oscartbeaumont/specta/issues/60
assert_ts!(Option<Option<Option<Option<i32>>>>, r#"number | null"#);

// assert_ts_export!(DeprecatedType, "");
// assert_ts_export!(DeprecatedTypeWithMsg, "");
// assert_ts_export!(DeprecatedFields, "");
Expand Down

0 comments on commit 216cf15

Please sign in to comment.