Skip to content
forked from olahol/tsreflect

Flexible reflection based TypeScript type generator for Go types that can be marshalled with `encoding/json`.

License

Notifications You must be signed in to change notification settings

ortfo/tsreflect-1

 
 

Repository files navigation

tsreflect

Build Status Codecov Go Report Card GoDoc

Flexible reflection based TypeScript type generator for Go types that can be marshalled with encoding/json.

Install

go get github.com/olahol/tsreflect

Example

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":""}
}

About

Flexible reflection based TypeScript type generator for Go types that can be marshalled with `encoding/json`.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%