From 216cf157ac7c93f8de5c83911ccbd820309b1e09 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Mon, 24 Apr 2023 15:23:34 +0800 Subject: [PATCH] fixes #60 - nesting of options --- src/lang/ts/mod.rs | 10 +++++++++- tests/ts.rs | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lang/ts/mod.rs b/src/lang/ts/mod.rs index 7cb4018..9067be1 100644 --- a/src/lang/ts/mod.rs +++ b/src/lang/ts/mod.rs @@ -151,7 +151,15 @@ fn datatype_inner(ctx: ExportContext, typ: &DataType) -> Result 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", diff --git a/tests/ts.rs b/tests/ts.rs index 1d2ff14..c941669 100644 --- a/tests/ts.rs +++ b/tests/ts.rs @@ -160,6 +160,9 @@ fn typescript_types() { // https://github.com/oscartbeaumont/specta/issues/65 assert_ts!(HashMap, r#"{ [key in "A" | "B"]: null }"#); + // https://github.com/oscartbeaumont/specta/issues/60 + assert_ts!(Option>>>, r#"number | null"#); + // assert_ts_export!(DeprecatedType, ""); // assert_ts_export!(DeprecatedTypeWithMsg, ""); // assert_ts_export!(DeprecatedFields, "");