go2ts

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2021 License: MIT Imports: 4 Imported by: 1

README

go2ts

Build Status Coverage Standard README pkg.go.dev reference golang version Go Report Card

Convert golang types to Typescript declarations.

Note:

  • chan T is converted to AsyncIterable<T>.
  • Interfaces are converted to any.
  • struct methods are NOT converted, but Converter.ConfigureFunc can be used to create method declarations.
  • Recursion is NOT supported.
  • By default:
    • Assumes functions/methods are async so return values are all Promise<T> and errors assumed to be thrown not returned.
    • context.Context in function parameters is ignored.
    • If a function returns multiple values they are returned as an array.

Install

go get github.com/alanshaw/go2ts

Usage

Example
package main

import (
  "reflect"
  "github.com/alanshaw/go2ts"
)

type User struct {
  Name string
}

func main () {
  c := go2ts.NewConverter()

  c.Convert(reflect.TypeOf("")) // string
  c.Convert(reflect.TypeOf(User{})) // { Name: string }
  c.Convert(reflect.TypeOf(func(string, int, bool) User { return nil })
  // (str: string, int: number, bool: boolean) => Promise<{ Name: string }>

  // Add custom type declarations
  c.AddTypes(map[reflect.Type]string{
    reflect.TypeOf(User{}): "User"
  })

  // Output now includes "User" instead of { Name: string }
  c.Convert(reflect.TypeOf(map[string]User{})) // { [k: string]: User }

  // Configuration for the function declarations:
  c.ConfigureFunc = func(t reflect.Type) FuncConf {
    return FuncConf{
      IsSync: true, // do not wrap return values in Promise<T>
      AlwaysArray: true, // always return an array of return values even if there's only 1
      NoIgnoreContext: true, // don't ignore the context.Context param
      ParamNames: []string{"ctx"}, // ordered parameter names
      // Also...
      // IsMethod: true,
      // MethodName: "MyMethod"
    }
  }
  c.Convert(reflect.TypeOf(func(context.Context) User { return nil })
  // (ctx: any) => [User]
}

API

pkg.go.dev Reference

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw

Documentation

Overview

Package go2ts converts golang types to Typescript declarations.

Example:

package main

import (
	"reflect"
	"github.com/alanshaw/go2ts"
)

type User struct {
	Name string
}

func main () {
	c := go2ts.NewConverter()

	c.Convert(reflect.TypeOf("")) // string
	c.Convert(reflect.TypeOf(User{})) // { Name: string }
	c.Convert(reflect.TypeOf(func(string, int, bool) User { return nil })
	// (str: string, int: number, bool: boolean) => Promise<{ Name: string }>

	// Add custom type declarations
	c.AddTypes(map[reflect.Type]string{
		reflect.TypeOf(User{}): "User"
	})

	// Output now includes "User" instead of { Name: string }
	c.Convert(reflect.TypeOf(map[string]User{})) // { [k: string]: User }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Converter

type Converter struct {

	// OnConvert is called when a type is converted but NOT present in the types
	// table. It is safe (and expected) that Converter.AddTypes is called from
	// this handler so that discovered types can be included in a converted type.
	OnConvert func(reflect.Type, string)
	// ConfigureFunc is called for each function that is converted in order to set
	// configuration options for how the typescript declaration should appear.
	ConfigureFunc func(reflect.Type) FuncConf
	// contains filtered or unexported fields
}

Converter will convert a golang reflect.Type to Typescript type string.

func NewConverter

func NewConverter() *Converter

NewConverter creates a new converter instance with primitive types added.

func (*Converter) AddParamNames

func (c *Converter) AddParamNames(customParamNames map[reflect.Type]string)

AddParamNames adds custom function parameter names for types.

func (*Converter) AddTypes

func (c *Converter) AddTypes(customTypes map[reflect.Type]string)

AddTypes adds custom types.

func (*Converter) Convert

func (c *Converter) Convert(t reflect.Type) (ts string)

Convert takes a golang reflect.Type and returns a Typescript type string.

Notes:

chan is converted to AsyncIterable.

Assumes functions/methods are async so return values are all Promise<T> and errors assumed to be thrown not returned.

If a function returns multiple values they are returned as an array.

Context in function params is ignored.

Recursion is NOT supported.

Interfaces are converted to any.

struct methods are NOT converted, but Converter.ConfigureFunc can be used to create method declarations.

type FuncConf added in v0.2.0

type FuncConf struct {
	// IsSync flags that the func is synchronous and returns values instead of a
	// Promise that resolves to the return values.
	IsSync bool
	// AlwaysArray will cause an array to be returned, even if there is only a
	// single return value. By default an array is only returned if there are 2+
	// return values.
	AlwaysArray bool
	// NoIgnoreContext will include a context.Context param in the typescript
	// function declaration if it is the first parameter. Default is to ignore it.
	NoIgnoreContext bool
	// IsMethod flags that the func is a method with a (ignored) receiver param
	// and causes the converter to output a class method declaration.
	IsMethod bool
	// MethodName is the name of the method, used when FuncConf.IsMethod is true.
	MethodName string
	// ParamNames overrides default or global parameter names.
	ParamNames []string
}

FuncConf are configuration options that determine how a function is converted into a typescript declaration by the converter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL