MCPcopy Index your code
hub / github.com/Aleph-Alpha/ts-rs

github.com/Aleph-Alpha/ts-rs @v12.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v12.0.0 ↗ · + Follow
720 symbols 891 edges 108 files 63 documented · 9% updated 21d agov12.0.0 · 2026-01-31★ 1,83518 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ts-rs

logo ts-rs

Generate typescript type declarations from rust types

actions status Crates.io version docs.rs docs Download

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 and the docs.

Get started

[dependencies]
ts-rs = "12.0"
#[derive(ts_rs::TS)]
#[ts(export)]
struct User {
    user_id: i32,
    first_name: String,
    last_name: String,
}

When running cargo test or cargo test export_bindings, the following TypeScript type will be exported to bindings/User.ts:

export type User = { user_id: number, first_name: string, last_name: string, };

Features

  • generate type declarations from rust structs
  • generate union declarations from rust enums
  • works with generic types
  • compatible with serde
  • generate necessary imports when exporting to multiple files
  • precise control over generated types

If there's a type you're dealing with which doesn't implement TS, you can use either #[ts(as = "..")] or #[ts(type = "..")], enable the appropriate cargo feature, or open a PR.

Configuration

When using #[ts(export)] on a type, ts-rs generates a test which writes the bindings for it to disk.\ The following environment variables may be set to configure how and where: | Variable | Description | Default | |--------------------------|---------------------------------------------------------------------|--------------| | TS_RS_EXPORT_DIR | Base directory into which bindings will be exported | ./bindings | | TS_RS_IMPORT_EXTENSION | File extension used in import statements | none | | TS_RS_LARGE_INT | Binding used for large integer types (i64, u64, i128, u128) | bigint |

We recommend putting this configuration in the project's config.toml to make it persistent:

# <project-root>/.cargo/config.toml
[env]
TS_RS_EXPORT_DIR = { value = "bindings", relative = true }
TS_RS_LARGE_INT = "number"

To export bindings programmatically without the use of tests, TS::export_all, TS::export, and TS::export_to_string can be used instead.

Serde Compatibility

With the serde-compat feature (enabled by default), serde attributes are parsed for enums and structs.\ Supported serde attributes: rename, rename-all, rename-all-fields, tag, content, untagged, skip, skip_serializing, skip_serializing_if, flatten, default

Note: skip_serializing and skip_serializing_if only have an effect when used together with #[serde(default)]. This ensures that the generated type is correct for both serialization and deserialization.

Note: skip_deserializing is ignored. If you wish to exclude a field from the generated type, but cannot use #[serde(skip)], use #[ts(skip)] instead.

When ts-rs encounters an unsupported serde attribute, a warning is emitted, unless the feature no-serde-warnings is enabled.\ We are currently waiting for #54140, which will improve the ergonomics arund these diagnostics.

Cargo Features

Feature Description
serde-compat Enabled by default

See the "serde compatibility" section below for more information. | | format | Enables formatting of the generated TypeScript bindings.

Currently, this unfortunately adds quite a few dependencies. | | no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered.

Enabling this feature silences these warnings. | | serde-json-impl | Implement TS for types from serde_json | | chrono-impl | Implement TS for types from chrono | | bigdecimal-impl | Implement TS for types from bigdecimal | | url-impl | Implement TS for types from url | | uuid-impl | Implement TS for types from uuid | | bson-uuid-impl | Implement TS for bson::oid::ObjectId and bson::uuid | | bytes-impl | Implement TS for types from bytes | | indexmap-impl | Implement TS for types from indexmap | | ordered-float-impl | Implement TS for types from ordered_float | | heapless-impl | Implement TS for types from heapless | | semver-impl | Implement TS for types from semver | | smol_str-impl | Implement TS for types from smol_str | | tokio-impl | Implement TS for types from tokio | | jiff-impl | Implement TS for types from jiff | | arrayvec-impl | Implement TS for types from arrayvec |

Contributing

Contributions are always welcome! Feel free to open an issue, discuss using GitHub discussions or open a PR. See CONTRIBUTING.md

MSRV

The Minimum Supported Rust Version for this crate is 1.88.0

License: MIT

Extension points exported contracts — how you extend this code

TS (Interface)
A type which can be represented in TypeScript. Most of the time, you'd want to derive this trait instead of implementing [9 …
ts-rs/src/lib.rs
Attr (Interface)
(no doc) [4 implementers]
macros/src/attr/mod.rs
Driver (Interface)
(no doc) [2 implementers]
ts-rs/tests/integration/concrete_generic.rs
TypeVisitor (Interface)
A visitor used to iterate over all dependencies or generics of a type. When an instance of [`TypeVisitor`] is passed to [3 …
ts-rs/src/lib.rs
ContainerAttr (Interface)
(no doc) [2 implementers]
macros/src/attr/mod.rs
Bar (Interface)
(no doc) [1 implementers]
ts-rs/tests/integration/infer_as.rs
IsOption (Interface)
(no doc) [1 implementers]
ts-rs/src/lib.rs
Driver (Interface)
(no doc) [1 implementers]
ts-rs/tests/integration/bound.rs

Core symbols most depended-on inside this repo

push
called by 34
macros/src/deps.rs
or
called by 29
macros/src/optional.rs
crate_rename
called by 13
macros/src/attr/enum.rs
assert_validity
called by 9
macros/src/attr/enum.rs
append_from
called by 5
macros/src/deps.rs
type_as
called by 5
macros/src/attr/field.rs
import_extension
called by 5
ts-rs/src/lib.rs
make_string_literal
called by 4
macros/src/utils.rs

Shape

Class 280
Function 243
Enum 99
Method 89
Interface 9

Languages

Rust100%

Modules by API surface

ts-rs/tests/integration/generics.rs42 symbols
ts-rs/src/lib.rs41 symbols
ts-rs/tests/integration/optional_field.rs28 symbols
ts-rs/tests/integration/enum_flattening_nested.rs20 symbols
ts-rs/tests/integration/top_level_type_as.rs18 symbols
ts-rs/tests/integration/struct_rename.rs18 symbols
ts-rs/tests/integration/docs.rs18 symbols
ts-rs/tests/integration/issue_308.rs17 symbols
macros/src/attr/mod.rs17 symbols
ts-rs/tests/integration/enum_flattening.rs16 symbols
e2e/workspace/parent/src/main.rs16 symbols
macros/src/lib.rs15 symbols

For agents

$ claude mcp add ts-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page