You can override the generated TypeScript type for a struct field using the tstype tag in Go.
Custom Type Mapping
Use tstype to provide a literal TypeScript type string.
type Book struct {
Title string `json:"title"`
Genre string `json:"genre" tstype:"'novel' | 'crime' | 'fantasy'"`
}
Required Fields
By default, pointer types in Go become optional (?) in TypeScript. To make them required, add ,required to the tag.
type Nicknames struct {
Alice *string `json:"alice"`
Bob *string `json:"bob" tstype:"BobCustomType,required"`
Charlie *string `json:"charlie" tstype:",required"`
}
Readonly Fields
To make a field immutable in TypeScript, add ,readonly to the tag.
type Cat struct {
Name string `json:"name,readonly"`
Owner string `json:"owner"`
}
Omit Fields
Use tstype:"-" to omit a field from the TypeScript output.