Can typescript-go Generate TypeScript Interfaces from Go Structs?
No, typescript-go cannot generate TypeScript interfaces from Go structs; it is a pure Go implementation of the TypeScript compiler designed only to parse, type-check, and emit JavaScript and declaration files from TypeScript source code.
The microsoft/typescript-go repository contains a native Go port of the TypeScript compiler toolchain. While it handles TypeScript-to-JavaScript compilation and .d.ts generation internally using Go data structures, it does not expose any functionality to convert user-defined Go structs into TypeScript interface definitions.
How typescript-go Processes Type Definitions
The compiler follows the standard TypeScript pipeline implemented entirely in Go. It ingests .ts and .tsx files, builds a type system around the parsed AST, and optionally emits declaration files.
The TypeScript-to-Declaration Pipeline
When you run tsgo with the --emitDeclarationOnly flag, the compiler executes a specific sequence located across several key packages:
- Parsing:
internal/parser/parser.goscans TypeScript source and produces an AST representation. - Type-Checking:
internal/compiler/program.gocoordinates the checker (internal/checker) to build a type graph from the parsed nodes. - Serialization:
internal/transformers/tstransforms/typeserializer.gowalks the TypeScript type objects and prints them as TypeScript syntax suitable for.d.tsfiles. - Emission:
internal/execute/tsc/emit.gowrites the final JavaScript and declaration files to disk.
This pipeline is strictly source-to-target for TypeScript only. The type serializer in typeserializer.go understands TypeScript-specific type constructs (unions, intersections, mapped types) but has no awareness of Go package definitions or struct tags.
Why Go Structs Cannot Be Used as Input
Go structs appear throughout the typescript-go codebase, but exclusively as implementation details of the compiler itself. For example, the AST nodes in internal/parser, symbol tables in internal/checker, and compiler options are all defined as Go structs. However, these are internal data structures, not a public schema definition language.
The compiler never includes a Go parser or a reflection-based type extractor that could interpret a user-defined struct like this:
type Person struct {
Name string
Age int
}
Because internal/parser/parser.go only understands TypeScript grammar, feeding it Go source code results in a parse error. There is no translation layer that converts Go type semantics (struct fields, tags, methods) into equivalent TypeScript interfaces.
Generating TypeScript Interfaces from Go Types
If you need to generate TypeScript interfaces from Go structs, you must use a dedicated code generation tool rather than the TypeScript compiler. You have two primary approaches:
- Write a custom generator using Go's standard
go/astandgo/typespackages to traverse your struct definitions and emit TypeScript syntax. - Use external tools specifically designed for this workflow, such as
go2ts,tsgen, or similar libraries that understand Go struct tags and can map Go types to their TypeScript equivalents (e.g.,stringtostring,inttonumber).
Example: Valid typescript-go Usage
To generate declaration files with typescript-go, you must start with TypeScript source. The CLI entry point in cmd/tsgo/main.go accepts the --emitDeclarationOnly flag:
# Emit only .d.ts files from TypeScript source
./tsgo --emitDeclarationOnly myfile.ts
# Creates: myfile.d.ts (and myfile.js depending on configuration)
This command utilizes the emission logic in internal/execute/tsc/emit.go, which relies on the type serializer to produce valid TypeScript declarations. It cannot process .go files.
Summary
- typescript-go is a TypeScript-to-JavaScript compiler, not a Go-to-TypeScript converter.
- Declaration file generation (
.d.ts) via--emitDeclarationOnlyonly works with TypeScript input parsed byinternal/parser/parser.go. - Go structs exist only as internal implementation structures; the compiler cannot parse user Go code or extract types from it.
- For Go-to-TypeScript interface generation, use dedicated external tools or write a custom AST walker using Go's
go/astpackage.
Frequently Asked Questions
Can typescript-go convert Go types to TypeScript automatically?
No, typescript-go does not support automatic conversion of Go types to TypeScript. The compiler operates exclusively on TypeScript source files (.ts/.tsx) and has no capability to parse Go struct definitions or reflect on Go types to generate corresponding TypeScript interfaces.
How do I generate .d.ts declaration files with typescript-go?
You generate declaration files by compiling TypeScript source with the --emitDeclarationOnly flag. As implemented in cmd/tsgo/main.go and internal/execute/tsc/emit.go, this triggers the declaration emitter which uses internal/transformers/tstransforms/typeserializer.go to serialize the TypeScript type graph into .d.ts syntax. The input must always be TypeScript, not Go source code.
What is the purpose of the typescript-go project?
The microsoft/typescript-go repository is a native Go reimplementation of the TypeScript compiler. Its purpose is to provide faster parsing and type-checking performance while maintaining compatibility with the official TypeScript compiler (tsc). It handles the full compilation pipeline from TypeScript to JavaScript, including bundling and minification features, but remains strictly a TypeScript toolchain.
Is there any way to use Go structs as schema definitions with typescript-go?
No, you cannot use Go structs as schema definitions within typescript-go. While the compiler is written in Go and uses structs to represent AST nodes and compiler state internally, it does not expose an API to ingest Go struct definitions as type schemas. You would need to convert your Go structs to TypeScript interfaces using an external code generator before typescript-go can process them.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →