Skip to content

fix(gen): better typing for optional types in typescript client #1876

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/clientgen/testdata/goapp/expected_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export namespace svc {
}

export interface Recursive {
Optional?: Recursive
Optional?: Recursive | undefined | null
Slice: Recursive[]
Map: { [key: string]: Recursive }
}
Expand All @@ -232,17 +232,17 @@ export namespace svc {
/**
* Foo is good
*/
Foo?: Foo
Foo?: Foo | undefined | null

/**
* Baz is better
*/
boo: string

QueryFoo?: boolean
QueryBar?: string
HeaderBaz?: string
HeaderInt?: number
QueryFoo?: boolean | undefined | null
QueryBar?: string | undefined | null
HeaderBaz?: string | undefined | null
HeaderInt?: number | undefined | null
/**
* This is a multiline
* comment on the raw message!
Expand Down
24 changes: 12 additions & 12 deletions pkg/clientgen/testdata/tsapp/expected_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,42 @@ export interface ClientOptions {

export namespace svc {
export interface AuthParams {
cookie?: string
token?: string
cookie?: string | undefined | null
token?: string | undefined | null
}

export interface Request {
/**
* Foo is good
*/
foo?: number
foo?: number | undefined | null

/**
* Baz is better
*/
baz: string

queryFoo?: boolean
queryBar?: string
headerBaz?: string
headerNum?: number
queryFoo?: boolean | undefined | null
queryBar?: string | undefined | null
headerBaz?: string | undefined | null
headerNum?: number | undefined | null
}

export interface Request {
/**
* Foo is good
*/
foo?: number
foo?: number | undefined | null

/**
* Baz is better
*/
baz: string

queryFoo?: boolean
queryBar?: string
headerBaz?: string
headerNum?: number
queryFoo?: boolean | undefined | null
queryBar?: string | undefined | null
headerBaz?: string | undefined | null
headerNum?: number | undefined | null
}

export class ServiceClient {
Expand Down
6 changes: 5 additions & 1 deletion pkg/clientgen/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -1744,11 +1744,15 @@ func (ts *typescript) writeTyp(ns string, typ *schema.Type, numIndents int) {
indent()
ts.WriteString(ts.QuoteIfRequired(ts.fieldNameInStruct(field)))

if field.Optional || ts.isRecursive(field.Typ) {
isOptional := field.Optional || ts.isRecursive(field.Typ)
if isOptional {
ts.WriteString("?")
}
ts.WriteString(": ")
ts.writeTyp(ns, field.Typ, numIndents+1)
if isOptional {
ts.WriteString(" | undefined | null")
}
ts.WriteString("\n")

// Add another empty line if we have a doc comment
Expand Down