forked from goplus/xgo
-
Notifications
You must be signed in to change notification settings - Fork 4
TypeScript Type System vs. Go
xushiwei edited this page Mar 31, 2026
·
1 revision
| TypeScript 类型 | 示例 | Go 对应类型 | 示例 | 说明 |
|---|---|---|---|---|
| 基础类型 | ||||
number |
let n: number = 42 |
int / float64
|
var n int = 42 |
TS 不区分整数/浮点,Go 有明确区分 |
string |
let s: string = "hi" |
string |
var s string = "hi" |
几乎一致 |
boolean |
let b: boolean = true |
bool |
var b bool = true |
名称略有不同 |
bigint |
let n: bigint = 9007n |
int64 / big.Int
|
var n int64 |
大整数,Go 用 math/big
|
symbol |
Symbol("key") |
❌ 无直接对应 | — | Go 无 Symbol 概念 |
null |
let x: null = null |
nil (指针/接口) |
var p *int = nil |
Go 的 nil 只用于引用类型 |
undefined |
let x: undefined |
❌ 无直接对应 | — | Go 无 undefined,零值代替 |
| 字面量类型 | ||||
| 字符串字面量 | type Dir = "left" | "right" |
const + iota / 自定义类型 |
type Dir string |
Go 用常量枚举模拟 |
| 数字字面量 | type One = 1 |
const |
const One = 1 |
— |
| 布尔字面量 | type T = true |
❌ 无直接对应 | — | Go 无布尔字面量类型 |
| 特殊类型 | ||||
any |
let x: any = "hi" |
interface{} / any
|
var x any = "hi" |
Go 1.18+ any 是 interface{} 别名 |
unknown |
let x: unknown |
interface{} + 类型断言 |
var x interface{} |
使用前需类型断言,语义相近 |
never |
function fail(): never |
❌ 无直接对应 | — | Go 用 panic 或无限循环代替 |
void |
function f(): void |
— (无返回值) | func f() |
Go 函数直接省略返回类型 |
object |
let o: object = {} |
struct{} / interface{}
|
var o interface{} |
— |
| 复合类型 | ||||
| Array |
number[] / Array<number>
|
[]T |
[]int |
Go 切片语义,非固定长度 |
| Tuple | [string, number] |
struct |
struct{ A string; B int } |
Go 无原生元组,用结构体 |
| 固定长度数组 |
[3] (不常用) |
[N]T |
[3]int |
Go 数组有固定长度区分 |
object / Record |
{ name: string } |
struct |
type P struct { Name string } |
Go 用具名结构体 |
Record<K,V> |
Record<string, number> |
map[K]V |
map[string]int |
— |
| Map | Map<K,V> |
map[K]V |
map[string]int |
— |
| Set | Set<T> |
map[T]struct{} |
map[int]struct{} |
Go 用 map 模拟 Set |
| 函数类型 | ||||
| 函数类型 | (a: number) => string |
func(int) string |
var f func(int) string |
Go 函数是一等公民 |
| 可选参数 | function f(x?: number) |
不支持,用零值或指针 | func f(x *int) |
— |
| 剩余参数 | ...args: number[] |
变参 | func f(args ...int) |
— |
| 函数重载 | function f(x: string): void |
❌ 不支持 | — | Go 无函数重载 |
| 泛型 | ||||
| 泛型类型 | Array<T> |
[]T (Go 1.18+) |
func F[T any](v T) |
Go 1.18 引入泛型 |
| 泛型约束 | T extends string |
T interface{ ... } |
[T fmt.Stringer] |
Go 用接口作约束 |
| 条件类型 | T extends U ? A : B |
❌ 无直接对应 | — | Go 无条件类型 |
infer |
infer R |
❌ 无对应 | — | Go 无类型推断于泛型内 |
| 联合 & 交叉类型 | ||||
| 联合类型 | string | number |
interface{} + 类型断言 |
— | Go 可用接口或 tagged union 模拟 |
| 可辨识联合 | { kind: "A" } | { kind: "B" } |
interface + switch
|
type Shape interface{} |
Go 用接口 + 类型 switch |
| 交叉类型 | A & B |
接口组合 / 嵌套结构体 | type C struct{ A; B } |
Go 用结构体嵌入组合 |
| 接口 & 类型别名 | ||||
interface |
interface Foo { bar(): void } |
interface |
type Foo interface{ Bar() } |
Go 接口是隐式实现 |
type 别名 |
type ID = string |
type |
type ID string |
Go 的 type 创建新类型,非别名 |
| 类 | ||||
class |
class Animal {} |
struct + 方法 |
type Animal struct{} |
Go 无 class,用结构体+方法模拟 |
| 继承 | class B extends A |
结构体嵌入 | type B struct{ A } |
Go 无继承,用组合 |
| 访问修饰符 |
public / private / protected
|
首字母大/小写 |
Name vs name
|
Go 用导出/非导出控制可见性 |
| 抽象类 | abstract class |
interface |
type Abs interface{} |
— |
| 枚举 | ||||
enum |
enum Color { Red, Green } |
iota + 自定义类型 |
type Color int; const Red Color = iota |
— |
const enum |
const enum Dir { Up } |
同上(编译期常量) | const Up = 0 |
— |
| 工具类型 | ||||
Partial<T> |
Partial<User> |
所有字段用指针 | type User struct { Name *string } |
— |
Required<T> |
Required<User> |
所有字段非指针 | type User struct { Name string } |
— |
Readonly<T> |
Readonly<User> |
❌ 无直接对应 | — | Go 无只读结构体,用不可变惯例 |
Pick<T,K> |
Pick<User, "id"> |
新定义结构体 | type UserID struct { ID int } |
需手动定义 |
Omit<T,K> |
Omit<User, "pass"> |
新定义结构体 | — | 需手动定义 |
Record<K,V> |
Record<string, number> |
map[K]V |
map[string]int |
— |
ReturnType<T> |
ReturnType<typeof fn> |
❌ 无对应 | — | Go 无元类型操作 |
keyof |
keyof User |
❌ 无对应 | — | Go 无反射级类型操作(运行时用 reflect) |
typeof |
typeof user |
reflect.TypeOf() |
reflect.TypeOf(u) |
Go 的 typeof 只在运行时 |
Awaited<T> |
Awaited<Promise<string>> |
❌ 无对应 | — | Go 用 channel / goroutine,无 Promise |
| 并发相关 | ||||
Promise<T> |
Promise<string> |
chan T / Future
|
ch := make(chan string, 1) |
Go 用 channel 或 goroutine |
async/await |
async function f() |
goroutine + channel
|
go func() { ch <- result }() |
并发模型完全不同 |
- TypeScript 的结构化类型系统(Structural Typing) vs Go 的隐式接口实现 — 两者都是鸭子类型,但 Go 更显式。
-
联合类型 是 TS 的核心特性,Go 无原生支持,通常用
interface{}+ 类型断言/switch 模拟。 -
null/undefined在 Go 中统一由零值(zero value)处理,不存在 undefined。 -
泛型 Go 1.18 才引入,表达能力比 TS 弱,无条件类型、
infer等高级特性。 -
工具类型(
Partial、Pick等)Go 中均需手动定义新结构体,无元编程支持。