5 unstable releases
| 0.3.0 | Nov 24, 2025 |
|---|---|
| 0.2.1 | Nov 23, 2025 |
| 0.2.0 | Nov 21, 2025 |
| 0.1.1 | Nov 21, 2025 |
| 0.1.0 | Nov 21, 2025 |
#1873 in Web programming
27KB
627 lines
schema-bridge
A minimal, practical Rust library for generating TypeScript type definitions from Rust types.
Features
- Simple to use: Just derive
SchemaBridgeon your types - Serde compatible: Works with serde attributes
- Comprehensive std support: Extensive support for Rust standard library types
- Minimal dependencies: No heavy tooling required
Installation
Add to your Cargo.toml:
[dependencies]
schema-bridge = "0.2"
serde = { version = "1.0", features = ["derive"] }
Quick Start
use schema_bridge::{SchemaBridge, export_types};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, SchemaBridge)]
struct User {
name: String,
age: i32,
email: Option<String>,
}
#[derive(Serialize, Deserialize, SchemaBridge)]
enum Status {
Active,
Inactive,
}
fn main() {
export_types!("bindings.ts", User, Status).unwrap();
}
This generates bindings.ts:
// This file is auto-generated by schema-bridge
export type User = {
name: string;
age: number;
email: string | null;
};
export type Status = "Active" | "Inactive";
Supported Types
Primitive Types
| Rust Type | TypeScript Type |
|---|---|
String |
string |
bool |
boolean |
char |
string |
() (unit) |
null |
Numeric Types
All numeric types map to TypeScript's number:
| Rust Type | TypeScript Type |
|---|---|
i8, i16, i32, i64, i128, isize |
number |
u8, u16, u32, u64, u128, usize |
number |
f32, f64 |
number |
Collection Types
| Rust Type | TypeScript Type | Example |
|---|---|---|
Vec<T> |
T[] |
Vec<string> → string[] |
Option<T> |
T | null |
Option<i32> → number | null |
HashMap<K, V> |
Record<K, V> |
HashMap<String, i32> → Record<string, number> |
BTreeMap<K, V> |
Record<K, V> |
BTreeMap<String, bool> → Record<string, boolean> |
HashSet<T> |
T[] |
HashSet<String> → string[] |
BTreeSet<T> |
T[] |
BTreeSet<i32> → number[] |
Smart Pointers
Smart pointers are transparent - they map to their inner type:
| Rust Type | TypeScript Type | Example |
|---|---|---|
Box<T> |
T |
Box<String> → string |
Rc<T> |
T |
Rc<i32> → number |
Arc<T> |
T |
Arc<String> → string |
Path Types
| Rust Type | TypeScript Type |
|---|---|
PathBuf |
string |
Result and Union Types
| Rust Type | TypeScript Type | Example |
|---|---|---|
Result<T, E> |
T | E |
Result<String, String> → string | string |
Tuple Types
Tuples (up to 6 elements) map to TypeScript tuple types:
| Rust Type | TypeScript Type |
|---|---|
(T,) |
[T] |
(T1, T2) |
[T1, T2] |
(T1, T2, T3) |
[T1, T2, T3] |
| ... | ... |
(T1, ..., T6) |
[T1, ..., T6] |
Advanced Examples
Complex Types
use schema_bridge::SchemaBridge;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
#[derive(SchemaBridge)]
pub struct ComplexType {
// Numeric types
pub id: u64, // number
pub score: f32, // number
// Collections
pub tags: HashSet<String>, // string[]
pub metadata: HashMap<String, i32>, // Record<string, number>
// Smart pointers
pub shared: Arc<String>, // string
// Tuples
pub coordinates: (f64, f64), // [number, number]
// Paths
pub file_path: PathBuf, // string
// Complex combinations
pub data: Option<Vec<Arc<String>>>, // string[] | null
}
Generates:
export type ComplexType = {
id: number;
score: number;
tags: string[];
metadata: Record<string, number>;
shared: string;
coordinates: [number, number];
file_path: string;
data: string[] | null;
};
Serde Attributes
schema-bridge respects serde attributes:
use schema_bridge::SchemaBridge;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, SchemaBridge)]
#[serde(rename_all = "camelCase")]
pub struct ApiResponse {
user_name: String, // becomes userName in TypeScript
is_active: bool, // becomes isActive in TypeScript
}
Architecture
This library is organized as a workspace with the following crates:
- schema-bridge: Main crate (re-exports everything)
- schema-bridge-core: Core traits and types
- schema-bridge-macro: Derive macro implementation
License
This project is licensed under the MIT License.
Dependencies
~0.5–1.4MB
~31K SLoC