Repository: 1Password/typeshare
Pull request #52 (2023-01-11 07:42:09)

Fix typescript optional type alias formatting
Typescript only allows ? in properties and arguments, not in type aliases.
In type aliases I've replaced it with " | undefined".

This is a potential fix for https://github.com/1Password/typeshare/issues/21


Patch:
diff --git a/core/src/language/typescript.rs b/core/src/language/typescript.rs
index c6f4380..adc2dc0 100644
--- a/core/src/language/typescript.rs
+++ b/core/src/language/typescript.rs
@@ -77,13 +77,16 @@ impl Language for TypeScript {
 
         writeln!(
             w,
-            "export type {}{}{} = {};\n",
+            "export type {}{} = {}{};\n",
             ty.id.renamed,
             (!ty.generic_types.is_empty())
                 .then(|| format!("<{}>", ty.generic_types.join(", ")))
                 .unwrap_or_default(),
-            ty.r#type.is_optional().then(|| "?").unwrap_or_default(),
             r#type,
+            ty.r#type
+                .is_optional()
+                .then(|| " | undefined")
+                .unwrap_or_default(),
         )?;
 
         Ok(())


Test patch:
diff --git a/core/data/tests/test_optional_type_alias/input.rs b/core/data/tests/test_optional_type_alias/input.rs
new file mode 100644
index 0000000..f26ca0d
--- /dev/null
+++ b/core/data/tests/test_optional_type_alias/input.rs
@@ -0,0 +1,11 @@
+#[typeshare]
+pub type OptionalU32 = Option<u32>;
+
+#[typeshare]
+pub struct OptionalU16(Option<u16>);
+
+#[typeshare]
+pub struct FooBar {
+    foo: OptionalU32,
+    bar: OptionalU16,
+}
diff --git a/core/data/tests/test_optional_type_alias/output.go b/core/data/tests/test_optional_type_alias/output.go
new file mode 100644
index 0000000..f6c8433
--- /dev/null
+++ b/core/data/tests/test_optional_type_alias/output.go
@@ -0,0 +1,13 @@
+// Code generated by typeshare 1.0.0. DO NOT EDIT.
+package proto
+
+import "encoding/json"
+
+type OptionalU32 *uint32
+
+type OptionalU16 *int
+
+type FooBar struct {
+	Foo OptionalU32 `json:"foo"`
+	Bar OptionalU16 `json:"bar"`
+}
diff --git a/core/data/tests/test_optional_type_alias/output.kt b/core/data/tests/test_optional_type_alias/output.kt
new file mode 100644
index 0000000..4cef919
--- /dev/null
+++ b/core/data/tests/test_optional_type_alias/output.kt
@@ -0,0 +1,21 @@
+/**
+ * Generated by typeshare 1.0.0
+ */
+
+@file:NoLiveLiterals
+
+package com.agilebits.onepassword
+
+import androidx.compose.runtime.NoLiveLiterals
+import kotlinx.serialization.*
+
+typealias OptionalU32 = UInt?
+
+typealias OptionalU16 = UShort?
+
+@Serializable
+data class FooBar (
+	val foo: OptionalU32,
+	val bar: OptionalU16
+)
+
diff --git a/core/data/tests/test_optional_type_alias/output.swift b/core/data/tests/test_optional_type_alias/output.swift
new file mode 100644
index 0000000..26253eb
--- /dev/null
+++ b/core/data/tests/test_optional_type_alias/output.swift
@@ -0,0 +1,19 @@
+/*
+ Generated by typeshare 1.0.0
+ */
+
+import Foundation
+
+public typealias OptionalU32 = UInt32?
+
+public typealias OptionalU16 = UInt16?
+
+public struct FooBar: Codable {
+	public let foo: OptionalU32
+	public let bar: OptionalU16
+
+	public init(foo: OptionalU32, bar: OptionalU16) {
+		self.foo = foo
+		self.bar = bar
+	}
+}
diff --git a/core/data/tests/test_optional_type_alias/output.ts b/core/data/tests/test_optional_type_alias/output.ts
new file mode 100644
index 0000000..53ea9b7
--- /dev/null
+++ b/core/data/tests/test_optional_type_alias/output.ts
@@ -0,0 +1,13 @@
+/*
+ Generated by typeshare 1.0.0
+*/
+
+export type OptionalU32 = number | undefined;
+
+export type OptionalU16 = number | undefined;
+
+export interface FooBar {
+	foo: OptionalU32;
+	bar: OptionalU16;
+}
+
diff --git a/core/tests/snapshot_tests.rs b/core/tests/snapshot_tests.rs
index 038303e..7726ced 100644
--- a/core/tests/snapshot_tests.rs
+++ b/core/tests/snapshot_tests.rs
@@ -438,6 +438,7 @@ tests! {
         },
     ];
     test_type_alias: [ swift { prefix: "OP".to_string(), }, kotlin, typescript, go ];
+    test_optional_type_alias: [swift, kotlin, typescript, go];
     test_serialized_as: [ swift { prefix: "OP".to_string(), }, kotlin, typescript ];
     test_serialized_as_tuple: [
         swift {