End-to-End types between Go and TypeScript
MkUnion enables the generation of TypeScript definitions directly from your Go union types. This facilitates end-to-end type safety when building applications with a Go backend and a TypeScript frontend.
By using the mkunion
tool, you can ensure that the data structures exchanged between your Go server and TypeScript client are consistent, reducing the likelihood of integration errors and improving developer experience.
The following snippet shows an example of Go code from which TypeScript definitions can be generated:
example/my-app/server.go
// this command make sure that all types that are imported will have generated typescript mapping
//go:generate ../../cmd/mkunion/mkunion shape-export --language=typescript -o ./src/workflow
// this lines defines all types that should have typescript mapping generated by above command
type (
Workflow = workflow.Workflow
State = workflow.State
Command = workflow.Command
Expr = workflow.Expr
Predicate = workflow.Predicate
Reshaper = workflow.Reshaper
Schema = schema.Schema
UpdateRecords = schemaless.UpdateRecords[schemaless.Record[any]]
FindRecords = schemaless.FindingRecords[schemaless.Record[any]]
PageResult = schemaless.PageResult[schemaless.Record[any]]
FunctionOutput = workflow.FunctionOutput
FunctionInput = workflow.FunctionInput
)
//go:tag mkunion:"ChatCMD"
type (
UserMessage struct {
This generated TypeScript code can then be imported into your frontend project, providing compile-time checks and autocompletion for your API responses and requests.