From 84f5fe2c00d8f43bc1e69f6206437e30fa61cd43 Mon Sep 17 00:00:00 2001 From: clearloop Date: Wed, 4 Mar 2020 14:13:59 +0800 Subject: [PATCH] add: tests for typescript_type attribute --- crates/typescript-tests/src/lib.rs | 1 + .../typescript-tests/src/typescript_type.rs | 37 +++++++++++++++++++ .../typescript-tests/src/typescript_type.ts | 9 +++++ 3 files changed, 47 insertions(+) create mode 100644 crates/typescript-tests/src/typescript_type.rs create mode 100644 crates/typescript-tests/src/typescript_type.ts diff --git a/crates/typescript-tests/src/lib.rs b/crates/typescript-tests/src/lib.rs index 5336adf4..6c6db10e 100644 --- a/crates/typescript-tests/src/lib.rs +++ b/crates/typescript-tests/src/lib.rs @@ -5,4 +5,5 @@ pub mod opt_args_and_ret; pub mod optional_fields; pub mod simple_fn; pub mod simple_struct; +pub mod typescript_type; pub mod web_sys; diff --git a/crates/typescript-tests/src/typescript_type.rs b/crates/typescript-tests/src/typescript_type.rs new file mode 100644 index 00000000..38e8742f --- /dev/null +++ b/crates/typescript-tests/src/typescript_type.rs @@ -0,0 +1,37 @@ +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) { + // parse JsValue + } + + pub fn optional_new(_i: Option) -> TextStyle { + // parse JsValueo + TextStyle::default() + } +} diff --git a/crates/typescript-tests/src/typescript_type.ts b/crates/typescript-tests/src/typescript_type.ts new file mode 100644 index 00000000..a05ada63 --- /dev/null +++ b/crates/typescript-tests/src/typescript_type.ts @@ -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();