Tech News
← Back to articles

Show HN: Guts – convert Golang types to TypeScript

read original related products more articles

Go Unto Ts (guts)

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

How to use it

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 () // 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 )

... continue reading