File tree 5 files changed +105
-0
lines changed
crates/typescript-tests/src
reference/attributes/on-rust-exports
5 files changed +105
-0
lines changed Original file line number Diff line number Diff line change @@ -5,4 +5,5 @@ pub mod opt_args_and_ret;
5
5
pub mod optional_fields;
6
6
pub mod simple_fn;
7
7
pub mod simple_struct;
8
+ pub mod typescript_type;
8
9
pub mod web_sys;
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 ( ) ;
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