Skip to content

Commit 003dc45

Browse files
committed
add: docs for typescript_type
1 parent 84f5fe2 commit 003dc45

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

crates/typescript-tests/src/typescript_type.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ pub struct TextStyle {
2626
#[wasm_bindgen]
2727
impl TextStyle {
2828
#[wasm_bindgen(constructor)]
29-
pub fn new(_i: ITextStyle) {
29+
pub fn new(_i: ITextStyle) -> TextStyle {
3030
// parse JsValue
31+
TextStyle::default()
3132
}
3233

3334
pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
34-
// parse JsValueo
35+
// parse JsValue
3536
TextStyle::default()
3637
}
3738
}

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)