Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Generate TS client union types from Go types #2035

@backevik

Description

@backevik

As far as I understand, it is not possible to generate union types when generating a TS client library from a Go application. Here's an example:

Input

type Season string

const (
  SeasonSummer Season = "Summer"
  SeasonAutumn Season = "Autumn"
  SeasonWinter Season = "Winter"
  SeasonSpring Season = "Spring"
)

type SomeType struct {
  Season Season `json:"season"`
}

Expected output

export interface SomeType {
  season: "Summer" | "Autumn" | "Winter" | "Spring"
}

Actual output

export interface SomeType {
  season: string
}

I.e. in order to strongly type season in my TypeScript application (as in my Go API), I would need to write a custom union type and hence maintain it both in TS and Go which defeats the purpose of generating a type for this example. Ideally, the generated TS type would be a union type but a TS enum would also be OK:

enum Season {
  SeasonSummer Season = "Summer"
  SeasonAutumn Season = "Autumn"
  SeasonWinter Season = "Winter"
  SeasonSpring Season = "Spring"
}

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions