# Can typescript-go Generate TypeScript Interfaces from Go Structs?

> Discover if typescript-go generates TypeScript interfaces from Go structs. Learn its true purpose: a Go implementation of the TypeScript compiler for JavaScript and declaration file generation.

- Repository: [Microsoft/typescript-go](https://github.com/microsoft/typescript-go)
- Tags: how-to-guide
- Published: 2026-04-25

---

**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`](https://github.com/microsoft/typescript-go/blob/main/.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:

1. **Parsing**: [`internal/parser/parser.go`](https://github.com/microsoft/typescript-go/blob/main/internal/parser/parser.go) scans TypeScript source and produces an AST representation.
2. **Type-Checking**: [`internal/compiler/program.go`](https://github.com/microsoft/typescript-go/blob/main/internal/compiler/program.go) coordinates the checker (`internal/checker`) to build a type graph from the parsed nodes.
3. **Serialization**: [`internal/transformers/tstransforms/typeserializer.go`](https://github.com/microsoft/typescript-go/blob/main/internal/transformers/tstransforms/typeserializer.go) walks the TypeScript type objects and prints them as TypeScript syntax suitable for [`.d.ts`](https://github.com/microsoft/typescript-go/blob/main/.d.ts) files.
4. **Emission**: [`internal/execute/tsc/emit.go`](https://github.com/microsoft/typescript-go/blob/main/internal/execute/tsc/emit.go) writes the final JavaScript and declaration files to disk.

This pipeline is strictly **source-to-target** for TypeScript only. The type serializer in [`typeserializer.go`](https://github.com/microsoft/typescript-go/blob/main/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:

```go
type Person struct {
    Name string
    Age  int
}

```

Because [`internal/parser/parser.go`](https://github.com/microsoft/typescript-go/blob/main/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/ast` and `go/types` packages 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., `string` to `string`, `int` to `number`).

### 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`](https://github.com/microsoft/typescript-go/blob/main/cmd/tsgo/main.go) accepts the `--emitDeclarationOnly` flag:

```bash

# 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`](https://github.com/microsoft/typescript-go/blob/main/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`](https://github.com/microsoft/typescript-go/blob/main/.d.ts)) via `--emitDeclarationOnly` only works with TypeScript input parsed by [`internal/parser/parser.go`](https://github.com/microsoft/typescript-go/blob/main/internal/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/ast` package.

## 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`](https://github.com/microsoft/typescript-go/blob/main/cmd/tsgo/main.go) and [`internal/execute/tsc/emit.go`](https://github.com/microsoft/typescript-go/blob/main/internal/execute/tsc/emit.go), this triggers the declaration emitter which uses [`internal/transformers/tstransforms/typeserializer.go`](https://github.com/microsoft/typescript-go/blob/main/internal/transformers/tstransforms/typeserializer.go) to serialize the TypeScript type graph into [`.d.ts`](https://github.com/microsoft/typescript-go/blob/main/.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.