Tsify

Tsify is a library for generating TypeScript definitions from rust code.

Using this with wasm-bindgen will automatically output the types to .d.ts.

Inspired by typescript-definitions and ts-rs.

Example

Click to show Cargo.toml.

toml [dependencies] tsify = "0.1" serde = { version = "1.0", features = ["derive"] } wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }

```rust use serde::{Deserialize, Serialize}; use tsify::Tsify; use wasm_bindgen::prelude::*;

[derive(Tsify, Serialize, Deserialize)]

[tsify(intowasmabi, fromwasmabi)]

pub struct Point { x: i32, y: i32, }

[wasm_bindgen]

pub fn into_js() -> Point { Point { x: 0, y: 0 } }

[wasm_bindgen]

pub fn from_js(point: Point) {} ```

Will generate the following .d.ts file:

ts /* tslint:disable */ /* eslint-disable */ /** * @returns {Point} */ export function into_js(): Point; /** * @param {Point} point */ export function from_js(point: Point): void; export type Point = { x: number; y: number };

Attributes

Tsify container attributes

Tsify field attributes

Serde attributes

Type Override

```rust use tsify::Tsify;

[derive(Tsify)]

pub struct Foo { #[tsify(type = "0 | 1 | 2")] x: i32, } ```

Generated type:

ts export type Foo = { x: 0 | 1 | 2 };

Optional Properties

```rust

[derive(Tsify)]

struct Optional { #[tsify(optional)] a: Option, #[serde(skipserializingif = "Option::is_none")] b: Option, #[serde(default)] c: i32, } ```

Generated type:

ts export type Optional = { a?: number; b?: string; c?: number };

Crate Features