Back to godoc.org
github.com/skia-dev/go2ts

package go2ts

v1.3.1
Latest Go to latest
Published: Jun 25, 2020 | License: BSD-3-Clause | Module: github.com/skia-dev/go2ts

README

go2ts

Go

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.

Inspired by https://github.com/OneOfOne/struct2ts.

This module does not supply a command-line interface. Just write a short Go program like the example below to generate your TypeScript files.

Install

go get github.com/skia-dev/go2ts

Example

Input:

import (
	"image/color"
	"log"
	"os"

	"github.com/skia-dev/go2ts"
)

func main() {
	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  `json:"Coordinates"`
		Color     color.Alpha
		Direction Direction
	}

	generator := go2ts.New()
	generator.Add(Turtle{})
	generator.AddUnion(AllDirections)
	generator.Render(os.Stdout)
}

Output:

// DO NOT EDIT. This file is automatically generated.

export interface Position {
  X: number;
  Y: number;
}

export interface Alpha {
  A: number;
}

export interface Turtle {
  Coordinates: Position;
  Color: Alpha;
  Direction: Direction;
}

export type Direction = 'up' | 'down' | 'left' | 'right';
Source: github.com/skia-dev/go2ts@v1.3.1/README.md