| Crates.io | to-ts |
| lib.rs | to-ts |
| version | 0.1.8 |
| created_at | 2026-01-23 16:13:13.923819+00 |
| updated_at | 2026-01-25 17:56:48.922326+00 |
| description | A lightweight tool to generate TypeScript definitions from Rust structs, enums, and constants. |
| homepage | |
| repository | https://github.com/cyl/to-ts |
| max_upload_size | |
| id | 2064894 |
| size | 20,560 |
轻量级 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 |
MIT