Giter Site home page Giter Site logo

tsify's Introduction

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.
[dependencies]
tsify = "0.4.5"
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2" }
use serde::{Deserialize, Serialize};
use tsify::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.

Crate Features

  • 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.

Attributes

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

  • type
  • optional

Serde attributes

  • rename
  • rename-all
  • tag
  • content
  • untagged
  • skip
  • skip_serializing
  • skip_deserializing
  • skip_serializing_if = "Option::is_none"
  • flatten
  • default
  • transparent

Type Override

use tsify::Tsify;

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

Generated type:

export interface Foo {
  x: 0 | 1 | 2;
}

Optional Properties

#[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;
}

Enum

#[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 } };

Enum with namespace

#[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 } };

Type Aliases

use tsify::{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>;

tsify's People

Contributors

madonoharu avatar 28smiles avatar dtolnay avatar steos avatar christophe-petitjean avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.