guts
is a tool to convert golang types to typescript for enabling a consistent type definition across the frontend and backend. It is intended to be called and customized as a library, rather than as a command line executable.
See the simple example for a 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 is to allow configuration with code, and also helps with package resolution.
See the simple example for a basic usage of the library. A larger example can be found in the Coder repository.
// Step 1: Create a new Golang parser
golang, _ := guts.NewGolangParser()
// 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. The Go AST is traversed to find all the types defined in the packages.
These types are placed into a simple AST that directly maps to the typescript AST.
Using goja, these types are then serialized to typescript using the typescript compiler API.
The generator aims to do the bare minimum type conversion. An example of a common opinion, is to create enum lists.
export type Enum = "bar" | "baz" | "foo" | "qux" // <-- Golang type
export const Enums: Enum[] = ["bar", "baz", "foo", "qux"] // <-- Helpful additional generated type
These kinds of opinions can be added with:
ts.ApplyMutations(
config.EnumLists,
)
output, _ := ts.Serialize()
An incredible website to visualize the AST of typescript: https://ts-ast-viewer.com/