展示 HN:Guts – 将 Golang 类型转换为 TypeScript
Show HN: Guts – convert Golang types to TypeScript

原始链接: https://github.com/coder/guts

## Guts:前往 TypeScript 代码生成 Guts 是一个 Go 库,旨在将 Go 类型转换为 TypeScript,从而在前端和后端之间保持类型一致性。与命令行工具不同,Guts 优先考虑程序化配置和自定义,允许开发者通过代码定制转换过程。 该过程涉及解析 Go 包,遍历其抽象语法树 (AST),并将 Go 类型映射到等效的 TypeScript AST 表示。然后,Guts 利用官方 TypeScript 编译器 API 将这些结构序列化为有效的 TypeScript 代码。 Guts 专注于最小的类型转换,提供变异来调整输出——例如,将 Go 类型转换为 TypeScript 枚举或类型别名。这种灵活性,加上它对 TypeScript 编译器的依赖,确保了语义上正确且最新的 TypeScript 定义。 Guts 旨在提供一个更动态和可配置的 Go 到 TypeScript 生成器替代方案。

## Guts:Go 类型到 TypeScript 类型生成 一个名为“Guts”的新工具(github.com/coder)允许开发者直接从 Go 类型自动生成 TypeScript 类型。在 Hacker News 上分享,一位用户称赞 Guts 已将其用于近一年的自定义 JSON-RPC API。 该工具的主要优势是简化 Go 后端和 TypeScript 前端之间的类型定义,减少手动同步和潜在错误。Guts 也具有高度可定制性,允许用户修改抽象语法树 (AST) 以进行定制的类型转换——例如将自定义的 Go 可选类型转换为 TypeScript 可选类型。 总而言之,它被描述为适用于需要在 Go 和 TypeScript 之间无缝共享类型的项目的“很棒的工具/库”。
相关文章

原文

Go Reference

guts is a tool to convert golang types to typescript for enabling a consistent type definition across the frontend and backend. It is intended to be called and customized as a library, rather than as a command line executable.

See the simple example for a basic usage of the library.

type SimpleType[T comparable] struct {
	FieldString     string
	FieldInt        int
	FieldComparable T
	FieldTime       time.Time
}

Gets converted into

type Comparable = string | number | boolean;

// From main/main.go
interface SimpleType<T extends Comparable> {
    FieldString: string;
    FieldInt: number;
    FieldComparable: T;
    FieldTime: string;
}

guts is a library, not a command line utility. This is to allow configuration with code, and also helps with package resolution.

See the simple example for a basic usage of the library. A larger example can be found in the Coder repository.

// Step 1: Create a new Golang parser
golang, _ := guts.NewGolangParser()

// Optional: Preserve comments from the golang source code
// This feature is still experimental and may not work in all cases
golang.PreserveComments()

// Step 2: Configure the parser
_ = golang.IncludeGenerate("github.com/coder/guts/example/simple")
// Step 3: Convert the Golang to the typescript AST
ts, _ := golang.ToTypescript()
// Step 4: Mutate the typescript AST
ts.ApplyMutations(
    config.ExportTypes, // add 'export' to all top level declarations
)
// Step 5: Serialize the typescript AST to a string
output, _ := ts.Serialize()
fmt.Println(output)

guts first parses a set of golang packages. The Go AST is traversed to find all the types defined in the packages.

These types are placed into a simple AST that directly maps to the typescript AST.

Using goja, these types are then serialized to typescript using the typescript compiler API.

The generator aims to do the bare minimum type conversion. An example of a common opinion, is to use types to represent enums. Without the mutation, the following is generated:

export enum EnumString {
    EnumBar = "bar",
    EnumBaz = "baz",
    EnumFoo = "foo",
    EnumQux = "qux"
}

Add the mutation:

ts.ApplyMutations(
	config.EnumAsTypes,
)
output, _ := ts.Serialize()

And the output is:

export type EnumString = "bar" | "baz" | "foo" | "qux";

The guts package was created to offer a more flexible, programmatic alternative to existing Go-to-TypeScript code generation tools out there.

The other solutions out there function as command-line utilities with yaml configurability. guts is a library, giving it a much more flexible and dynamic configuration that static generators can’t easily support.

Unlike many of its counterparts, guts leverages the official TypeScript compiler under the hood, ensuring that the generated TypeScript definitions are semantically correct, syntactically valid, and aligned with the latest language features.

An incredible website to visualize the AST of typescript: https://ts-ast-viewer.com/

联系我们 contact @ memedata.com