-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1986 from clearloop/master
Add typescript_type attribute
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
guide/src/reference/attributes/on-rust-exports/typescript_type.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |