From 1e249e46023a85deb89a398d36e30809307b71cb Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Wed, 16 Feb 2022 16:11:50 +0100 Subject: [PATCH] fix: use null and extend on types --- typescript.go | 24 +++++++++++++++++++----- typscriptclientasync.go | 20 ++++++++++---------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/typescript.go b/typescript.go index 19ac004..8ca9145 100644 --- a/typescript.go +++ b/typescript.go @@ -34,11 +34,16 @@ func (v *Value) tsType(mappings config.TypeScriptMappings, scalars map[string]*S ts.app(",") v.Map.Value.tsType(mappings, scalars, structs, ts) ts.app(">") + ts.app("|null") case v.Array != nil: + if v.Array.Value.ScalarType != ScalarTypeByte { + ts.app("Array<") + } v.Array.Value.tsType(mappings, scalars, structs, ts) if v.Array.Value.ScalarType != ScalarTypeByte { - ts.app("[]") + ts.app(">") } + ts.app("|null") case v.Scalar != nil: if v.Scalar.Package != "" { mapping, ok := mappings[v.Scalar.Package] @@ -89,6 +94,9 @@ func (v *Value) tsType(mappings config.TypeScriptMappings, scalars map[string]*S } ts.app(tsModule + "." + v.StructType.Name) + if v.IsPtr { + ts.app("|null") + } return } ts.app(v.StructType.Name) @@ -97,10 +105,19 @@ func (v *Value) tsType(mappings config.TypeScriptMappings, scalars map[string]*S ts.l("{").ind(1) renderStructFields(v.Struct.Fields, mappings, scalars, structs, ts) ts.ind(-1).app("}") + if v.IsPtr { + ts.app("|null") + } case len(v.ScalarType) > 0: ts.app(tsTypeFromScalarType(v.ScalarType)) + if v.IsPtr { + ts.app("|null") + } default: ts.app("any") + if v.IsPtr { + ts.app("|null") + } } return } @@ -121,10 +138,7 @@ func renderStructFields(fields []*Field, mappings config.TypeScriptMappings, sca continue } ts.app(f.tsName()) - if f.Value.IsPtr || - f.Value.Map != nil || - f.Value.Array != nil || - (f.JSONInfo != nil && f.JSONInfo.OmitEmpty) { + if f.JSONInfo != nil && f.JSONInfo.OmitEmpty { ts.app("?") } ts.app(":") diff --git a/typscriptclientasync.go b/typscriptclientasync.go index 5aead5f..ab69de5 100644 --- a/typscriptclientasync.go +++ b/typscriptclientasync.go @@ -48,14 +48,14 @@ func renderTypescriptClientAsync(service *Service, mappings config.TypeScriptMap ts.app(", ") } ts.app(arg.tsName()) + ts.app(":") + arg.Value.tsType(mappings, scalars, structs, ts) if arg.Value.IsPtr || arg.Value.Map != nil || arg.Value.Array != nil || (arg.JSONInfo != nil && arg.JSONInfo.OmitEmpty) { - ts.app("?") + ts.app("|null") } - ts.app(":") - arg.Value.tsType(mappings, scalars, structs, ts) callArgs = append(callArgs, arg.Name) argCount++ } @@ -97,11 +97,11 @@ func renderTypescriptClientAsync(service *Service, mappings config.TypeScriptMap } innerReturnTypeTS.app(strconv.Itoa(index)) - if isNilable { - innerReturnTypeTS.app("?") - } innerReturnTypeTS.app(":") retField.Value.tsType(mappings, scalars, structs, innerReturnTypeTS) + if isNilable { + innerReturnTypeTS.app("|null") + } if index == len(method.Return)-1 && retField.Value.IsError { throwLastError = true @@ -113,18 +113,18 @@ func renderTypescriptClientAsync(service *Service, mappings config.TypeScriptMap retField.Value.tsType(mappings, scalars, structs, firstReturnTypeTS) firstReturnType = firstReturnTypeTS.string() if isNilable { - firstReturnType += "|undefined" + firstReturnType += "|null" } //firstReturnFieldName = retArgName } countReturns++ returnTypeTS.app(retArgName) - if isNilable { - returnTypeTS.app("?") - } returnTypeTS.app(":") responseObject += responseObjectPrefix + retArgName + " : response[" + strconv.Itoa(index) + "]" retField.Value.tsType(mappings, scalars, structs, returnTypeTS) + if isNilable { + returnTypeTS.app("|null") + } } responseObjectPrefix = ", " }