Introduction to Tygo
Tygo is an intuitive tool designed to generate TypeScript typings directly from Golang source files. It streamlines the development process by effortlessly converting Golang types, preserving comments, recognizing constants, and supporting non-struct type expressions. This capability is particularly beneficial for developers who need to generate corresponding types for a Golang REST API to integrate seamlessly with a front-end codebase.
Key Features
- Support for Golang 1.18 Generics and Struct Inheritance: Tygo is equipped to handle complex types introduced in the latest versions of Golang, including generic types and struct inheritance.
- Comment Preservation: Comments from the Golang source are retained in the TypeScript output, ensuring that annotations and documentation remain intact across conversions.
- Constant Recognition: Tygo understands constant definitions, simplifying the process of type translation.
Installation
To install Tygo, enter the following command in your shell:
go install github.com/gzuidhof/tygo@latest
Usage
Option A: Command Line Interface (Recommended)
- Create a
tygo.yaml
configuration file where you can specify which packages should be converted and define any special type mappings. - Execute the command:
tygo generate
The resulting TypeScript file will be placed adjacent to your Go source files.
Option B: Library Mode
In library mode, developers can set up Tygo programmatically within their applications:
config := &tygo.Config{
Packages: []*tygo.PackageConfig{
&tygo.PackageConfig{
Path: "github.com/gzuidhof/tygo/examples/bookstore",
},
},
}
gen := tygo.New(config)
err := gen.Generate()
Configuration
Tygo offers extensive configuration options through a YAML file, allowing for:
- Custom type translations
- Output path specification
- Inclusion of additional file content through the
frontmatter
field - Exclusion of specific files from the conversion process
Type Hints and Custom Mapping
Developers can utilize the tstype
tag inside structs to define precise TypeScript output types. This feature supports complex custom mappings and readonly fields. Additionally, Tygo enables the use of directives like tygo:emit
for generating TypeScript code that isn’t directly mappable from Go.
Advanced Features
- Generic Support: Tygo natively supports Golang's generic types, introduced in version 1.18.
- Interface Inheritance: It allows for reflecting Go’s struct inheritance characteristics in TypeScript interfaces.
- YAML Serialization: Tygo can generate typings for YAML-serializable objects, reflecting Go’s typical serialization behavior.
Related Projects
Tygo joins a suite of tools aimed at bridging the gap between Go and TypeScript, including projects like typescriptify-golang-structs
and go2ts
. However, Tygo distinguishes itself by parsing source files directly, thus preserving original file characteristics such as comments without requiring additional struct tags.
Conclusion
If Tygo proves beneficial for your development needs, consider showing your support by giving the project a star on its repository.
Licensing
Tygo is distributed under the MIT License, offering flexibility for both personal and commercial use.