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
[]
= "0.5.5"
= { = "1.0", = ["derive"] }
= { = "0.2" }
use ;
use Tsify;
use *;
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.
Crate Features
json(default) enables serialization throughserde_json.jsenables serialization throughserde-wasm-bindgenand generates the appropriate types for it. This will be the default in future versions.
Attributes
Tsify container attributes
into_wasm_abiimplementsIntoWasmAbiandOptionIntoWasmAbi. This can be converted directly from Rust to JS viaserde_jsonorserde-wasm-bindgen.from_wasm_abiimplementsFromWasmAbiandOptionFromWasmAbi. This is the opposite operation of the above.namespacegenerates a namespace for the enum variants.typeoverrides at the container level.type_paramsoverrides params at the container level.
Serializer configuration options
missing_as_nullhashmap_as_objectlarge_number_types_as_bigints
Tsify field attributes
typetype_paramsoptional
Serde attributes
renamerename-alltagcontentuntaggedskipskip_serializingskip_deserializingskip_serializing_if = "Option::is_none"flattendefaulttransparent
Type Override
use Tsify;
Generated type:
export interface Foo {
x: 0 | 1 | 2;
}
Optional Properties
Generated type:
export interface Optional {
a?: number;
b?: string;
c?: number;
}
Enum
Generated type:
export type Color =
| "Red"
| "Blue"
| "Green"
| { Rgb: [number, number, number] }
| { Hsv: { hue: number; saturation: number; value: number } };
Enum with namespace
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 } };
Type Aliases
use ;
;
type Bar = ;
Generated type:
export type Foo<T> = T;
export type Bar = Foo<number>;