Skip to content

Add typescript_type attribute #1986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/typescript-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub mod opt_args_and_ret;
pub mod optional_fields;
pub mod simple_fn;
pub mod simple_struct;
pub mod typescript_type;
pub mod web_sys;
38 changes: 38 additions & 0 deletions crates/typescript-tests/src/typescript_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use wasm_bindgen::prelude::*;

#[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 {
// parse JsValue
TextStyle::default()
}

pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
// parse JsValue
TextStyle::default()
}
}
9 changes: 9 additions & 0 deletions crates/typescript-tests/src/typescript_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as wbg from '../pkg/typescript_tests';

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

const optional_style: wbg.TextStyle = wbg.TextStyle.optional_new();
1 change: 1 addition & 0 deletions guide/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
- [`getter` and `setter`](./reference/attributes/on-rust-exports/getter-and-setter.md)
- [`inspectable`](./reference/attributes/on-rust-exports/inspectable.md)
- [`skip_typescript`](./reference/attributes/on-rust-exports/skip_typescript.md)
- [`typescript_type`](./reference/attributes/on-rust-exports/typescript_type.md)

- [`web-sys`](./web-sys/index.md)
- [Using `web-sys`](./web-sys/using-web-sys.md)
Expand Down
56 changes: 56 additions & 0 deletions guide/src/reference/attributes/on-rust-exports/typescript_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 {
// parse JsValue
TextStyle::default()
}

pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
// parse JsValueo
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();
```