Skip to content

Commit 5acd6a3

Browse files
authored
Merge pull request #1986 from clearloop/master
Add typescript_type attribute
2 parents 8a3bdbd + 003dc45 commit 5acd6a3

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

crates/typescript-tests/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pub mod opt_args_and_ret;
55
pub mod optional_fields;
66
pub mod simple_fn;
77
pub mod simple_struct;
8+
pub mod typescript_type;
89
pub mod web_sys;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen(typescript_custom_section)]
4+
const ITEXT_STYLE: &'static str = r#"
5+
interface ITextStyle {
6+
bold: boolean;
7+
italic: boolean;
8+
size: number;
9+
}
10+
"#;
11+
12+
#[wasm_bindgen]
13+
extern "C" {
14+
#[wasm_bindgen(typescript_type = "ITextStyle")]
15+
pub type ITextStyle;
16+
}
17+
18+
#[wasm_bindgen]
19+
#[derive(Default)]
20+
pub struct TextStyle {
21+
pub bold: bool,
22+
pub italic: bool,
23+
pub size: i32,
24+
}
25+
26+
#[wasm_bindgen]
27+
impl TextStyle {
28+
#[wasm_bindgen(constructor)]
29+
pub fn new(_i: ITextStyle) -> TextStyle {
30+
// parse JsValue
31+
TextStyle::default()
32+
}
33+
34+
pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
35+
// parse JsValue
36+
TextStyle::default()
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as wbg from '../pkg/typescript_tests';
2+
3+
const style: wbg.TextStyle = new wbg.TextStyle({
4+
bold: true,
5+
italic: true,
6+
size: 42,
7+
});
8+
9+
const optional_style: wbg.TextStyle = wbg.TextStyle.optional_new();

guide/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
- [`getter` and `setter`](./reference/attributes/on-rust-exports/getter-and-setter.md)
8383
- [`inspectable`](./reference/attributes/on-rust-exports/inspectable.md)
8484
- [`skip_typescript`](./reference/attributes/on-rust-exports/skip_typescript.md)
85+
- [`typescript_type`](./reference/attributes/on-rust-exports/typescript_type.md)
8586

8687
- [`web-sys`](./web-sys/index.md)
8788
- [Using `web-sys`](./web-sys/using-web-sys.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# typescript_type
2+
3+
The `typescript_type` allows us to use typescript declarations in `typescript_custom_section` as arguments for rust functions! For example:
4+
5+
```rust
6+
#[wasm_bindgen(typescript_custom_section)]
7+
const ITEXT_STYLE: &'static str = r#"
8+
interface ITextStyle {
9+
bold: boolean;
10+
italic: boolean;
11+
size: number;
12+
}
13+
"#;
14+
15+
#[wasm_bindgen]
16+
extern "C" {
17+
#[wasm_bindgen(typescript_type = "ITextStyle")]
18+
pub type ITextStyle;
19+
}
20+
21+
#[wasm_bindgen]
22+
#[derive(Default)]
23+
pub struct TextStyle {
24+
pub bold: bool,
25+
pub italic: bool,
26+
pub size: i32,
27+
}
28+
29+
#[wasm_bindgen]
30+
impl TextStyle {
31+
#[wasm_bindgen(constructor)]
32+
pub fn new(_i: ITextStyle) -> TextStyle {
33+
// parse JsValue
34+
TextStyle::default()
35+
}
36+
37+
pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
38+
// parse JsValueo
39+
TextStyle::default()
40+
}
41+
}
42+
```
43+
44+
We can write our `typescript` code like:
45+
46+
```ts
47+
import { ITextStyle, TextStyle } from "./my_awesome_module";
48+
49+
const style: TextStyle = new TextStyle({
50+
bold: true,
51+
italic: true,
52+
size: 42,
53+
});
54+
55+
const optional_style: TextStyle = TextStyle.optional_new();
56+
```

0 commit comments

Comments
 (0)