olahol / tsreflect

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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`.

License:MIT License


Languages

Language:Go 100.0%