diff --git a/tools/cmd/gen-types/types.ts.tmpl b/tools/cmd/gen-types/types.ts.tmpl index 55fee850d..0814d75f7 100644 --- a/tools/cmd/gen-types/types.ts.tmpl +++ b/tools/cmd/gen-types/types.ts.tmpl @@ -10,7 +10,7 @@ import { encodeAs } from "../encoding"; {{ define "objectify"}} this.{{lcName .Name}} && {{- if .Repeatable -}} -this.{{lcName .Name}}?.map(v => {{objectify . "v"}}) +this.{{lcName .Name}}?.map(v => v == undefined ? undefined : {{objectify . "v"}}) {{- else}} {{objectify . (print "this." (lcName .Name))}} {{- end}} diff --git a/tools/cmd/gen-types/typescript.go b/tools/cmd/gen-types/typescript.go index 114ee3e71..8d6903c20 100644 --- a/tools/cmd/gen-types/typescript.go +++ b/tools/cmd/gen-types/typescript.go @@ -62,7 +62,7 @@ func TsNeedsCtor(typ *Type) bool { func TsResolveType(field *Field) string { typ := field.Type.TypescriptType(false) if field.Repeatable { - typ = typ + "[]" + typ = "(" + typ + " | undefined)[]" } return typ } @@ -84,7 +84,7 @@ func TsInputType(field *Field) string { typ = field.Type.TypescriptInputType(false) } if field.Repeatable { - typ = "(" + typ + ")[]" + typ = "(" + typ + " | undefined)[]" } return typ } @@ -117,7 +117,7 @@ func TsUnobjectify(field *Field, varName string) string { return tsUnobjectify2(field, varName) } - return fmt.Sprintf("%s.map(v => %s)", varName, tsUnobjectify2(field, "v")) + return fmt.Sprintf("%s.map(v => v == undefined ? undefined : %s)", varName, tsUnobjectify2(field, "v")) } func tsUnobjectify2(field *Field, varName string) string {