Documentation ¶
Overview ¶
Package go2ts converts golang types to Typescript declarations.
Example:
package main import ( "reflect" "github.com/alanshaw/go2ts" ) type User struct { Name string } func main () { c := go2ts.NewConverter() c.Convert(reflect.TypeOf("")) // string c.Convert(reflect.TypeOf(User{})) // { Name: string } c.Convert(reflect.TypeOf(func(string, int, bool) User { return nil }) // (str: string, int: number, bool: boolean) => Promise<{ Name: string }> // Add custom type declarations c.AddTypes(map[reflect.Type]string{ reflect.TypeOf(User{}): "User" }) // Output now includes "User" instead of { Name: string } c.Convert(reflect.TypeOf(map[string]User{})) // { [k: string]: User } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Converter ¶
type Converter struct { // OnConvert is called when a type is converted but NOT present in the types // table. It is safe (and expected) that Converter.AddTypes is called from // this handler so that discovered types can be included in a converted type. OnConvert func(reflect.Type, string) // ConfigureFunc is called for each function that is converted in order to set // configuration options for how the typescript declaration should appear. ConfigureFunc func(reflect.Type) FuncConf // contains filtered or unexported fields }
Converter will convert a golang reflect.Type to Typescript type string.
func NewConverter ¶
func NewConverter() *Converter
NewConverter creates a new converter instance with primitive types added.
func (*Converter) AddParamNames ¶
AddParamNames adds custom function parameter names for types.
func (*Converter) Convert ¶
Convert takes a golang reflect.Type and returns a Typescript type string.
Notes:
chan is converted to AsyncIterable.
Assumes functions/methods are async so return values are all Promise<T> and errors assumed to be thrown not returned.
If a function returns multiple values they are returned as an array.
Context in function params is ignored.
Recursion is NOT supported.
Interfaces are converted to any.
struct methods are NOT converted, but Converter.ConfigureFunc can be used to create method declarations.
type FuncConf ¶ added in v0.2.0
type FuncConf struct { // IsSync flags that the func is synchronous and returns values instead of a // Promise that resolves to the return values. IsSync bool // AlwaysArray will cause an array to be returned, even if there is only a // single return value. By default an array is only returned if there are 2+ // return values. AlwaysArray bool // NoIgnoreContext will include a context.Context param in the typescript // function declaration if it is the first parameter. Default is to ignore it. NoIgnoreContext bool // IsMethod flags that the func is a method with a (ignored) receiver param // and causes the converter to output a class method declaration. IsMethod bool // MethodName is the name of the method, used when FuncConf.IsMethod is true. MethodName string // ParamNames overrides default or global parameter names. ParamNames []string }
FuncConf are configuration options that determine how a function is converted into a typescript declaration by the converter.