18 releases (10 stable)

new 3.0.0 May 3, 2021
2.3.0 Apr 6, 2021
2.2.0 Jan 22, 2021
2.1.2 Dec 19, 2020
0.0.1 Dec 15, 2020

#414 in Rust patterns

Download history 95/week @ 2021-01-13 21/week @ 2021-01-20 3/week @ 2021-01-27 15/week @ 2021-02-03 61/week @ 2021-02-10 55/week @ 2021-02-17 52/week @ 2021-02-24 21/week @ 2021-03-03 3/week @ 2021-03-10 3/week @ 2021-03-17 51/week @ 2021-03-24 50/week @ 2021-03-31 28/week @ 2021-04-07 60/week @ 2021-04-14 59/week @ 2021-04-21 43/week @ 2021-04-28

157 downloads per month
Used in ts-rs

MIT license

26KB
720 lines

ts-rs

Generate TypeScript interface/type declarations from rust structs.

Crate API Test

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 trait, 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 = "2.4"
use ts_rs::{TS, export};

#[derive(TS)]
struct User {
    user_id: i32,
    first_name: String,
    last_name: String,
}

export! {
    User => "bindings.ts"
}

When running cargo test, the TypeScript bindings will be exported to the file bindings.ts.

example

features

  • generate interface declarations from rust structs
  • generate union declarations from rust enums
  • inline types
  • flatten structs/interfaces
  • generate necessary imports when exporting to multiple files

Serde Compatability

Serde representations are supported for enums and structs. The serde tags supported on enums are:

  • tag & content
  • untagged

todo

  • serde compatibility layer
  • documentation
  • use typescript types across files
  • more enum representations
  • don't require 'static

Dependencies

~0.5–1MB
~22K SLoC