Sha256: 1960cbeea1047a0fdb936e3af4556ae805e9777323076b9f3820cb3986f27de6

Contents?: true

Size: 1.15 KB

Versions: 16

Compression:

Stored size: 1.15 KB

Contents

# typescript_type

The `typescript_type` allows us to use typescript declarations in `typescript_custom_section` as arguments for rust functions! For example:

```rust
#[wasm_bindgen(typescript_custom_section)]
const ITEXT_STYLE: &'static str = r#"
interface ITextStyle {
    bold: boolean;
    italic: boolean;
    size: number;
}
"#;

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

#[wasm_bindgen]
#[derive(Default)]
pub struct TextStyle {
    pub bold: bool,
    pub italic: bool,
    pub size: i32,
}

#[wasm_bindgen]
impl TextStyle {
    #[wasm_bindgen(constructor)]
    pub fn new(i: ITextStyle) -> TextStyle {
        let _js_value: JsValue = i.into();
        // parse JsValue
        TextStyle::default()
    }

    pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
        // parse JsValue
        TextStyle::default()
    }
}
```

We can write our `typescript` code like: 

```ts
import { ITextStyle, TextStyle } from "./my_awesome_module";

const style: TextStyle = new TextStyle({
  bold: true,
  italic: true,
  size: 42,
});

const optional_style: TextStyle = TextStyle.optional_new();
```

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
wasmtime-14.0.4 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-14.0.3 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-14.0.1 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-14.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-13.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-12.0.1 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-12.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-11.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-10.0.1 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-10.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-9.0.4 ./ext/cargo-vendor/wasm-bindgen-0.2.87/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-9.0.1 ./ext/cargo-vendor/wasm-bindgen-0.2.86/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-8.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.84/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-7.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.84/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-6.0.1 ./ext/cargo-vendor/wasm-bindgen-0.2.84/guide/src/reference/attributes/on-rust-exports/typescript_type.md
wasmtime-6.0.0 ./ext/cargo-vendor/wasm-bindgen-0.2.84/guide/src/reference/attributes/on-rust-exports/typescript_type.md