Skip to content

Commit

Permalink
make unit values optional
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFoxPro committed Sep 13, 2024
1 parent 1ac5bed commit baf136a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions serde-generate/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,12 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
writeln!(self.out, "export type {name} = {{")?;
self.out.indent();
for field in fields {
writeln!(self.out, "{}: {},", field.name, self.quote_type(&field.value))?;
match field.value {
Format::Unit => {
writeln!(self.out, "{}?: {},", field.name, self.quote_type(&field.value))?;
}
_ => { writeln!(self.out, "{}: {},", field.name, self.quote_type(&field.value))?; }
}
}
self.out.unindent();
writeln!(self.out, "}}")?;
Expand All @@ -286,7 +291,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
for (_index, variant) in variants {
match &variant.value {
VariantFormat::Unit => {
writeln!(self.out, r#"| {{ $: "{0}", {0}: {1} }}"#, variant.name, self.quote_type(&Format::Unit))?;
writeln!(self.out, r#"| {{ $: "{0}", {0}?: {1} }}"#, variant.name, self.quote_type(&Format::Unit))?;
}
VariantFormat::Struct(fields) => {
let fields_str = fields.iter().map(|f| format!("{}: {}", f.name, self.quote_type(&f.value))).collect::<Vec<_>>().join(", ");
Expand Down
2 changes: 1 addition & 1 deletion suite/typescript/ts/bincode/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type MultiEnum =
| { $: "VariantA", VariantA: $t.i32 }
| { $: "VariantB", VariantB: $t.str }
| { $: "VariantC", VariantC: { x: $t.u8, y: $t.f64 } }
| { $: "UnitVariant", UnitVariant: $t.unit }
| { $: "UnitVariant", UnitVariant?: $t.unit }

export type NewtypeStruct = $t.i32

Expand Down

0 comments on commit baf136a

Please sign in to comment.