Flexible reflection based TypeScript type generator for Go types that can be marshalled with
encoding/json
.
go get github.com/olahol/tsreflect
package main
import (
"fmt"
"reflect"
"strings"
"github.com/olahol/tsreflect"
)
type MyStruct struct {
Number int
String int `json:",string"`
Alias string `json:"alias"`
Hidden string `json:"-"`
}
func main() {
g := tsreflect.New()
var x MyStruct
typ := reflect.TypeOf(x)
g.Add(typ)
value, _ := json.Marshal(x)
fmt.Println(g.DeclarationsTypeScript())
fmt.Printf("const x: %s = %s", g.TypeOf(typ), value)
// Output:
// interface MyStruct { "Number": number; "String": string; "alias": string; }
// const x: MyStruct = {"Number":0,"String":"0","alias":""}
}