coder/guts: Guts is a code generator that converts Golang types to Typescript. Useful for keeping types in sync between the front and backend.

go reference

guts There is a tool for converting Golang types to TypeScript to enable a consistent type definition in the frontend and backend. It is intended to be called and optimized as a library rather than a command line executable.

See simple examples for basic usage of the library.

type SimpleType[T comparable] struct {
	FieldString     string
	FieldInt        int
	FieldComparable T
	FieldTime       time.Time
}

gets converted into

type Comparable = string | number | boolean;

// From main/main.go
interface SimpleType<T extends Comparable> {
    FieldString: string;
    FieldInt: number;
    FieldComparable: T;
    FieldTime: string;
}

guts Is a library, not a command line utility. This allows configuration with code, and also helps with package resolution.

See simple examples for basic usage of the library. A large example can be found in the coder repository.

// Step 1: Create a new Golang parser
golang, _ := guts.NewGolangParser()

// Optional: Preserve comments from the golang source code
// This feature is still experimental and may not work in all cases
golang.PreserveComments()

// Step 2: Configure the parser
_ = golang.IncludeGenerate("github.com/coder/guts/example/simple")
// Step 3: Convert the Golang to the typescript AST
ts, _ := golang.ToTypescript()
// Step 4: Mutate the typescript AST
ts.ApplyMutations(
    config.ExportTypes, // add 'export' to all top level declarations
)
// Step 5: Serialize the typescript AST to a string
output, _ := ts.Serialize()
fmt.Println(output)

guts First parses a set of Golang packages. Go AST is traversed to find all types defined in packages.

These types are placed in a simple AST that maps directly to the TypeScript AST.

Using Gyoza, these types are serialized into TypeScript using the TypeScript compiler API.

The goal of the generator is to perform minimal type transformations. An example of consensus is to use types to represent enums. Without mutation, the following arises:

export enum EnumString {
    EnumBar = "bar",
    EnumBaz = "baz",
    EnumFoo = "foo",
    EnumQux = "qux"
}

Add mutation:

ts.ApplyMutations(
	config.EnumAsTypes,
)
output, _ := ts.Serialize()

And the output is:

export type EnumString = "bar" | "baz" | "foo" | "qux";

The Guts package was created to provide a more flexible, programmatic alternative to the existing go-to TypeScript code generation tools.

Other solutions out there act as command-line utilities with yaml configuration. guts There is a library, which gives it more flexible and dynamic configuration that static generators cannot easily support.

Unlike many of its counterparts, Guts takes advantage of the official TypeScript compiler under the hood, ensuring that the generated TypeScript definitions are semantically correct, syntactically valid, and aligned with the latest language features.

An incredible website for viewing TypeScript’s AST: https://ts-ast-viewer.com/



Leave a Comment