/go2ts

A Golang library to create TypeScript definitions from Go types.

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

THIS REPOSITORY IS ARCHIVED. Code moved to the Skia Infrastructure repository here (see CL).

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';