to-ts

Crates.ioto-ts
lib.rsto-ts
version0.1.8
created_at2026-01-23 16:13:13.923819+00
updated_at2026-01-25 17:56:48.922326+00
descriptionA lightweight tool to generate TypeScript definitions from Rust structs, enums, and constants.
homepage
repositoryhttps://github.com/cyl/to-ts
max_upload_size
id2064894
size20,560
(CylAlon)

documentation

README

to-ts

轻量级 Rust 到 TypeScript 类型定义生成器

安装

[dependencies]
to-ts = "0.1"

快速开始

use to_ts::ts;

#[ts]
pub struct User {
    pub id: u32,
    pub name: String,
}

#[ts]
pub enum Status {
    Active,
    Inactive,
}

#[ts]
pub const MAX_USERS: u32 = 1000;

// 在 main.rs 底部添加:
to_ts::ts_export_test!("src/generated/types.ts");

运行 cargo test 自动生成 types.ts

export interface User {
  id: number;
  name: string;
}

export type Status = "Active" | "Inactive";

export const MAX_USERS: number = 1000;

枚举格式

格式 用法 输出
联合类型(默认) #[ts] type E = "A" | "B"
字符串枚举 #[ts(enum = "string")] enum E { A = "A" }
数字枚举 #[ts(enum = "number")] enum E { A = 0 }

带元数据的枚举

使用 doc 注释定义变体元数据:

#[ts(enum = "number", meta)]
pub enum Priority {
    /// label = "低", color = "#00f"
    Low = 1,
    /// label = "高", color = "#f00"
    High = 2,
}

生成:

export enum Priority {
  Low = 1,
  High = 2,
}

export const PriorityMeta = {
  [Priority.Low]: { label: "低", color: "#00f" },
  [Priority.High]: { label: "高", color: "#f00" },
} as const;

支持的类型

Rust 类型 TypeScript 类型
u8-u64, i8-i64, f32, f64 number
bool boolean
String, &str, char string
Vec<T>, HashSet<T> Array<T>
HashMap<K,V> Record<K,V>
Option<T> T | null
Result<T,E> { Ok: T } | { Err: E }
Box<T>, Arc<T>, Rc<T> T(透明)
(A,B) [A,B]
PathBuf string
Duration number

License

MIT

Commit count: 0

cargo fmt