File tree 3 files changed +60
-2
lines changed
crates/typescript-tests/src
reference/attributes/on-rust-exports
3 files changed +60
-2
lines changed Original file line number Diff line number Diff line change @@ -26,12 +26,13 @@ pub struct TextStyle {
26
26
#[ wasm_bindgen]
27
27
impl TextStyle {
28
28
#[ wasm_bindgen( constructor) ]
29
- pub fn new ( _i : ITextStyle ) {
29
+ pub fn new ( _i : ITextStyle ) -> TextStyle {
30
30
// parse JsValue
31
+ TextStyle :: default ( )
31
32
}
32
33
33
34
pub fn optional_new ( _i : Option < ITextStyle > ) -> TextStyle {
34
- // parse JsValueo
35
+ // parse JsValue
35
36
TextStyle :: default ( )
36
37
}
37
38
}
Original file line number Diff line number Diff line change 82
82
- [ ` getter ` and ` setter ` ] ( ./reference/attributes/on-rust-exports/getter-and-setter.md )
83
83
- [ ` inspectable ` ] ( ./reference/attributes/on-rust-exports/inspectable.md )
84
84
- [ ` skip_typescript ` ] ( ./reference/attributes/on-rust-exports/skip_typescript.md )
85
+ - [ ` typescript_type ` ] ( ./reference/attributes/on-rust-exports/typescript_type.md )
85
86
86
87
- [ ` web-sys ` ] ( ./web-sys/index.md )
87
88
- [ Using ` web-sys ` ] ( ./web-sys/using-web-sys.md )
Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments