Skip to content

Commit baf136a

Browse files
committed
make unit values optional
1 parent 1ac5bed commit baf136a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

serde-generate/src/typescript.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
271271
writeln!(self.out, "export type {name} = {{")?;
272272
self.out.indent();
273273
for field in fields {
274-
writeln!(self.out, "{}: {},", field.name, self.quote_type(&field.value))?;
274+
match field.value {
275+
Format::Unit => {
276+
writeln!(self.out, "{}?: {},", field.name, self.quote_type(&field.value))?;
277+
}
278+
_ => { writeln!(self.out, "{}: {},", field.name, self.quote_type(&field.value))?; }
279+
}
275280
}
276281
self.out.unindent();
277282
writeln!(self.out, "}}")?;
@@ -286,7 +291,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
286291
for (_index, variant) in variants {
287292
match &variant.value {
288293
VariantFormat::Unit => {
289-
writeln!(self.out, r#"| {{ $: "{0}", {0}: {1} }}"#, variant.name, self.quote_type(&Format::Unit))?;
294+
writeln!(self.out, r#"| {{ $: "{0}", {0}?: {1} }}"#, variant.name, self.quote_type(&Format::Unit))?;
290295
}
291296
VariantFormat::Struct(fields) => {
292297
let fields_str = fields.iter().map(|f| format!("{}: {}", f.name, self.quote_type(&f.value))).collect::<Vec<_>>().join(", ");

suite/typescript/ts/bincode/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type MultiEnum =
1515
| { $: "VariantA", VariantA: $t.i32 }
1616
| { $: "VariantB", VariantB: $t.str }
1717
| { $: "VariantC", VariantC: { x: $t.u8, y: $t.f64 } }
18-
| { $: "UnitVariant", UnitVariant: $t.unit }
18+
| { $: "UnitVariant", UnitVariant?: $t.unit }
1919

2020
export type NewtypeStruct = $t.i32
2121

0 commit comments

Comments
 (0)