2020-12-19 04:39:15 +01:00
fmt
2020-12-19 03:58:35 +01:00
fmt
2020-12-19 03:58:35 +01:00
2020-12-19 03:58:14 +01:00
2020-12-15 16:06:53 +01:00
2020-12-15 15:21:33 +01:00
2020-12-19 04:39:15 +01:00
2020-12-19 03:58:14 +01:00

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.0"
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 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
  • documentation
  • use typescript types across files
  • more enum representations
  • don't require 'static
Description
fork of Aleph-Alpha/ts-rs; branch use-ts-enum pinned by vibe-kanban
Readme MIT 1.2 MiB
Languages
Rust 100%