TypeScript Interfaces to Go Structs
Paste TypeScript interfaces, type aliases and enums and get Go structs with json tags, omitempty for optional properties, pointers for nullable ones and typed constants for string enums.
Use the tool
Why this tool exists
A Go service that serves a TypeScript front end has to describe the same JSON twice, and the two descriptions drift. Starting from the TypeScript declarations gives the Go side the exact property names, so the json tags match what the client sends.
Optional properties become omitempty with a pointer, so a missing field and a zero value stay distinguishable; nullable properties become pointers; arrays become slices, Record becomes map[string]T, and Date becomes time.Time (or string). Names are exported in Go style with initialisms such as ID and URL. The output compiles with go vet and round-trips JSON through encoding/json. Everything runs in your browser and nothing is uploaded.
How to use it
- Paste your TypeScript interfaces, type aliases or enums.
- Set the package name and choose how number and Date map.
- Copy the Go code into a file and run gofmt if you edit it.
Frequently asked questions
Which TypeScript features are supported?
Interfaces, type aliases, string enums, optional and nullable properties, unions, literals, tuples, arrays, Record, Date, extends and intersections, and JSDoc comments. Conditional and mapped types, keyof, typeof, function types and most utility generics are not supported, and the tool says so instead of guessing.
What happens to a union such as string | number?
Go has no general union type, so the field becomes any with a comment that says it was a union. Unions of literal values such as "a" | "b" become the base type with the literal list in a comment, and a top-level string-literal type alias becomes a real enum.
Why does number become a floating point type?
TypeScript has a single number type, so the converter cannot know whether a value is an integer. It uses a floating point type by default. Switch the option or edit single fields where you know the value is whole.
How are extends and intersections handled?
Go struct embedding would change how the JSON is read, so the fields of the base types are copied into the struct instead. The base types are still emitted as their own structs.