go2ts

go2ts instantly converts Go structs into TypeScript types with no configuration required. Support edge cases like embed struct / interface{} / alias / time.Time with some specified db.
Features
- Parse Go struct definitions
- Supports nested, generic, and alias types
- Automatically generates
.ts type definition files
- CLI for easy integration into any Go project
Getting started
Prerequisites
Install
go install github.com/limbicnode/go2ts/cmd/go2ts@latest
Quick Start
# Basic usage with default paths
go2ts
# Specify custom input and output paths
go2ts -in ./internal/models -out ./types.ts
Build
git clone https://github.com/limbicnode/go2ts.git
cd go2ts
go build -o go2ts ./cmd/go2ts
./go2ts -in ./models -out ./types.ts
Usage
CLI Usage
go2ts -in <input-directory> -out <output-file>
Flags:
-in: Directory to scan Go structs (default: ./internal/model)
-out: Output TypeScript file path (default: types.ts)
Examples:
# Convert structs from models directory
go2ts -in ./internal/models -out ./types.ts
# Use default paths
go2ts
Package Usage
package main
import (
"log"
go2ts "github.com/limbicnode/go2ts/pkg/go2ts"
)
func main() {
// Convert Go structs to TypeScript types
if err := go2ts.Convert("./models", "./types.ts"); err != nil {
log.Fatal(err)
}
}
Go Struct (test/testdata/model/test_struct.go):
package models
import "time"
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type UserResponse struct {
User User `json:"user"`
Token string `json:"token"`
}
Generated TypeScript (types.ts):
// Generated by go2ts — 0000-00-00 00:00:00
export interface User {
id: string;
name: string;
email: string;
age: number;
created_at: string;
updated_at: string;
}
export interface UserResponse {
user: User;
token: string;
}
Make Commands
# Show all available commands
make help
# Format and organize code
make fmt imports
# Run tests
make test
# Run tests with coverage
make coverage
# Run all CI checks locally
make ci
# Build binary
make build
# Clean build artifacts
make clean
# Benchmark
make benchmark
Roadmap
- Support for more complex Go struct patterns
- Support for custom database-specific types
- Handle unique types used in various databases
- Enhance CLI interface
- Parsing docs from struct
Contribution
The internal/ packages contain implementation details and should not be imported directly by external users.
Please use the public API provided under pkg/go2ts for all external usage.
Contributions, bug reports, and feature requests are always welcome!
See CONTRIBUTING.md for guidelines.
License
MIT License © 2025 limbicnode