| Crates.io | tsify-next |
| lib.rs | tsify-next |
| version | 0.5.6 |
| created_at | 2024-04-10 21:33:50.846992+00 |
| updated_at | 2025-04-20 17:07:48.365375+00 |
| description | Tsify-next is a library for generating TypeScript definitions from rust code. |
| homepage | https://github.com/siefkenj/tsify |
| repository | https://github.com/siefkenj/tsify |
| max_upload_size | |
| id | 1204087 |
| size | 123,899 |
Tsify now has all the features of tsify-next; tsify-next has served its purpose and will no longer receive updates. Use tsify instead.
Tsify-next is a library for generating TypeScript definitions from Rust code. The original Tsify appears to be in hibernation mode. This repository will maintain updates until main Tsify project comes back to life.
Using this with wasm-bindgen will automatically output the types to .d.ts.
Inspired by typescript-definitions and ts-rs.
[dependencies]
tsify-next = "0.5.4"
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2" }
use serde::{Deserialize, Serialize};
use tsify_next::Tsify;
use wasm_bindgen::prelude::*;
#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
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:
/* tslint:disable */
/* eslint-disable */
/**
* @returns {Point}
*/
export function into_js(): Point;
/**
* @param {Point} point
*/
export function from_js(point: Point): void;
export interface Point {
x: number;
y: number;
}
This is the behavior due to typescript_custom_section and Rust Type conversions.
json (default) enables serialization through serde_json.js enables serialization through serde-wasm-bindgen and generates the appropriate types for it. This will be the default in future versions.Tsify container attributes
into_wasm_abi implements IntoWasmAbi and OptionIntoWasmAbi. This can be converted directly from Rust to JS via serde_json or serde-wasm-bindgen.from_wasm_abi implements FromWasmAbi and OptionFromWasmAbi. This is the opposite operation of the above.namespace generates a namespace for the enum variants.Tsify field attributes
typeoptionalSerde attributes
renamerename-alltagcontentuntaggedskipskip_serializingskip_deserializingskip_serializing_if = "Option::is_none"flattendefaulttransparentuse tsify_next::Tsify;
#[derive(Tsify)]
pub struct Foo {
#[tsify(type = "0 | 1 | 2")]
x: i32,
}
Generated type:
export interface Foo {
x: 0 | 1 | 2;
}
#[derive(Tsify)]
struct Optional {
#[tsify(optional)]
a: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<String>,
#[serde(default)]
c: i32,
}
Generated type:
export interface Optional {
a?: number;
b?: string;
c?: number;
}
#[derive(Tsify)]
enum Color {
Red,
Blue,
Green,
Rgb(u8, u8, u8),
Hsv {
hue: f64,
saturation: f64,
value: f64,
},
}
Generated type:
export type Color =
| "Red"
| "Blue"
| "Green"
| { Rgb: [number, number, number] }
| { Hsv: { hue: number; saturation: number; value: number } };
#[derive(Tsify)]
#[tsify(namespace)]
enum Color {
Red,
Blue,
Green,
Rgb(u8, u8, u8),
Hsv {
hue: f64,
saturation: f64,
value: f64,
},
}
Generated type:
declare namespace Color {
export type Red = "Red";
export type Blue = "Blue";
export type Green = "Green";
export type Rgb = { Rgb: [number, number, number] };
export type Hsv = {
Hsv: { hue: number; saturation: number; value: number };
};
}
export type Color =
| "Red"
| "Blue"
| "Green"
| { Rgb: [number, number, number] }
| { Hsv: { hue: number; saturation: number; value: number } };
use tsify_next::{declare, Tsify};
#[derive(Tsify)]
struct Foo<T>(T);
#[declare]
type Bar = Foo<i32>;
Generated type:
export type Foo<T> = T;
export type Bar = Foo<number>;