mirror of
https://github.com/xazukx/ts-rs.git
synced 2026-11-18 22:30:26 +00:00
4e9b3ad70815d64ea32572c32b3d6d5eb3f93a99
ts-rs
Generate TypeScript interface/type declarations from rust structs.
why?
When building a web application in rust, data structures have to be shared between backend and frontend.
Using this library, you can easily generate TypeScript bindings to your rust structs & enums, so that you can keep your
types in one place.
ts-rs might also come in handy when working with webassembly.
how?
ts-rs exposes a single interface, TS. Using a derive macro, you can implement this interface for your types.
Then, you can use this trait to obtain the TypeScript bindings.
We recommend doing this in your tests. see the example
get started
[dependencies]
ts-rs = "0.1"
use ts_rs::TS;
#[derive(TS)]
struct User {
user_id: i32,
first_name: String,
last_name: String,
}
#[test]
fn export_ts() {
std::fs::remove_file("bindings.ts").ok();
User::dump("bindings.ts").unwrap();
}
example
features
- generate interface declarations from rust structs
- generate union declarations from rust enums
- inline types
serde compatibility layer
With the serde-compat feature enabled, ts-rs tries parsing serde attributes.
Please note that not all serde attributes are supported yet.
todo
- serde compatibility layer
- more customization
- library support (chrono, uuids, ...)
- documentation
Languages
Rust
100%