Skip to content

ivnsch/export_ts_macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Macro to derive wasm-bindgen Typescript definitons from structs.

⚠️ Early WIP!

⚠️ Found library that does this: tsify -> stopping work.

Install

export_ts_macro={ git = "https://github.com/ivanschuetz/export_ts_macro", branch = "main" }

Usage

use export_ts_macro::export_ts;
use wasm_bindgen::prelude::*;

#[export_ts(FooTs)]
struct Foo {
    a: i32,
    b: String,
}

#[wasm_bindgen]
pub fn hello(pars: FooTs) {
    let js: JsValue = pars.into();
    let _actual: Foo = js.into_serde().unwrap();
}

This generates the following Typescript:

interface FooTs {
  a: i32;
  b: string;
}

/**
 * @param {FooTs} pars
 */
export function foo(pars: FooTs): void;

Internals

The macro generates code according to the typescript_type section in the wasm-bindgen guide.

#[wasm_bindgen(typescript_custom_section)]
const FooTs: &'static str = r#"
interface FooTs {
    a: number
    b: string
}
"#;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(typescript_type = "FooTs")]
    pub type FooTs;
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages