# README
gut
Convert Golang structs to Typescript interfaces
Handle
- json tags, like
omitempty
andinline
appropriately uuid.UUID
&time.Time
conversion- type generation for arrays
- setting custom names for the generated typescript interfaces
- types which use generics (see
examples/5-generic-struct
)
Install
go get github.com/tompston/gut
Example
main.go | example.gen.ts |
---|---|
|
|
To format the generated typescript code, you can run deno fmt .
Why?
I wanted to solve the problem of converting golang structs to typescript interfaces, so that the backend and frontend clients could be in-sync, but I found that the current packages which try to do this had bugs and did not have the features I wanted. So I had to write it from scratch (with some help from ChatGPT)
Test
go test . -v -count=1
Features
-
Handle cases when the convertable struct is an array
-
Handle json ",omitempty" tags
-
Handle json ",inline" tags (embeded structs)
- If a struct has a field with an json ",inline" tag, then the generated
typescript interface will have all of the fields from the embeded struct
inside of it. (see
examples/4-struct-with-inline
)
- If a struct has a field with an json ",inline" tag, then the generated
typescript interface will have all of the fields from the embeded struct
inside of it. (see
-
handle
uuid.UUID
&time.Time
conversion -
Avoid duplicate interface names, by generating only one typescript interface which will hold all of the types that are present in the struct.
-
Flexible typescript type system for the converted
time.Time
,uuid.UUID
andint64
/uint64
types. -
optionally generate the type which holds an array of interfaces
-
Ability to optionally rename the generated typescript interface to a custom name
-
flexibility in exporting the converted go structs from packages
Convert()
returns a string which holds the generated typescript interface, thus grouping the structs from multiple packages, concatenating them together and then saving them to a single file is quite trivial.
-
Keep the package simple.
gut
exports only 2 funtionsConvert()
-> converts the struct into a ts stringGenerate()
-> save the converted ts interfaces to a file + define the settings for the types
Disclaimer
- The generated TS code is not formatted. Maybe I'll fix this later. The current way to format can be done by using
deno fmt .
- There might be bugs.
Credits
The initial draft of the code was generated by ChatGPT. After some heavy refactoring, this is the end result.
Example 2 - Modify interface names + array types
main.go | example.gen.ts |
---|---|
|
|
Example 3 - Change the predefined typescript type values
main.go | example.gen.ts |
---|---|
|
|