Documentation ¶
Overview ¶
Package go2ts is an extremely simple and powerful Go to Typescript generator. It can handle all JSON serializable Go types and also has the ability to define TypeScript union types for your enum-like types.
Example ¶
type Direction string const ( Up Direction = "up" Down Direction = "down" Left Direction = "left" Right Direction = "right" ) AllDirections := []Direction{Up, Down, Left, Right} type Position struct { X int Y int } type Turtle struct { Position Position `json:"Coordinates"` Color color.Alpha Direction Direction } generator := New() err := generator.Add(Turtle{}) if err != nil { log.Fatal(err) } err = generator.AddUnion(AllDirections) if err != nil { log.Fatal(err) } err = generator.Render(os.Stdout) if err != nil { log.Fatal(err) }
Output: // DO NOT EDIT. This file is automatically generated. X: number; Y: number; } A: number; } Coordinates: Position; Color: Alpha; Direction: Direction; }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Go2TS ¶
type Go2TS struct {
// contains filtered or unexported fields
}
Go2TS writes TypeScript definitions for Go types.
func (*Go2TS) Add ¶
Add a type that needs a TypeScript definition.
See AddWithName() for more details.
func (*Go2TS) AddUnion ¶ added in v1.3.0
AddUnion adds a TypeScript definition for a union type of the values in 'v', which must be a slice or an array.
func (*Go2TS) AddUnionWithName ¶ added in v1.3.0
AddUnionWithName adds a TypeScript definition for a union type of the values in 'v', which must be a slice or an array.
If typeName is the empty string then the name of type of elements in the slice or array is used as the type name, otherwise the typeName supplied will be used as the TypeScript type name.
func (*Go2TS) AddWithName ¶
AddWithName adds a type that needs a TypeScript definition.
The value passed in can be an instance of a type, a reflect.Type, or a reflect.Value.
The 'name' supplied will be the TypeScript interface name. If 'interfaceName' is the empty string then the Go type name will be used. If the type is of a struct that is anonymous it will be given a name of the form "AnonymousN".
If the type is a struct, the fields of the struct will be named following the convention for json serialization, including using the json tag if supplied. Fields tagged with `json:",omitempty"` will have "| null" added to their type.
There is special handling of time.Time types to be TypeScript "string"s since time.Time implements MarshalJSON, see https://pkg.go.dev/time?tab=doc#Time.MarshalJSON.