mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-15 17:50:51 +00:00
add: docs for typescript_type
This commit is contained in:
parent
84f5fe2c00
commit
003dc45d76
@ -26,12 +26,13 @@ pub struct TextStyle {
|
||||
#[wasm_bindgen]
|
||||
impl TextStyle {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(_i: ITextStyle) {
|
||||
pub fn new(_i: ITextStyle) -> TextStyle {
|
||||
// parse JsValue
|
||||
TextStyle::default()
|
||||
}
|
||||
|
||||
pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
|
||||
// parse JsValueo
|
||||
// parse JsValue
|
||||
TextStyle::default()
|
||||
}
|
||||
}
|
||||
|
@ -82,6 +82,7 @@
|
||||
- [`getter` and `setter`](./reference/attributes/on-rust-exports/getter-and-setter.md)
|
||||
- [`inspectable`](./reference/attributes/on-rust-exports/inspectable.md)
|
||||
- [`skip_typescript`](./reference/attributes/on-rust-exports/skip_typescript.md)
|
||||
- [`typescript_type`](./reference/attributes/on-rust-exports/typescript_type.md)
|
||||
|
||||
- [`web-sys`](./web-sys/index.md)
|
||||
- [Using `web-sys`](./web-sys/using-web-sys.md)
|
||||
|
@ -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();
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user