mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-16 22:41:24 +00:00
Allow web-sys to emit correct typescript declarations from webidl (#1998)
* Update to emit typescript names * Update to use NamedAnyref * Update incoming / outgoing * Remove added space * Remove comment * Add basic typescript tests for web-sys
This commit is contained in:
@ -4,3 +4,4 @@ pub mod opt_args_and_ret;
|
||||
pub mod optional_fields;
|
||||
pub mod simple_fn;
|
||||
pub mod simple_struct;
|
||||
pub mod web_sys;
|
||||
|
24
crates/typescript-tests/src/web_sys.rs
Normal file
24
crates/typescript-tests/src/web_sys.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct DocStruct {
|
||||
doc: Document,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl DocStruct {
|
||||
pub fn new(doc: Document) -> Self {
|
||||
Self { doc }
|
||||
}
|
||||
|
||||
pub fn get_doc(&self) -> Document {
|
||||
self.doc.clone()
|
||||
}
|
||||
|
||||
pub fn append_element(&self, element: &HtmlElement) {
|
||||
self.doc.body().unwrap().append_child(element).unwrap();
|
||||
}
|
||||
|
||||
pub fn append_many(&self, _: &HtmlElement, _: &HtmlElement, _: &HtmlElement) {}
|
||||
}
|
26
crates/typescript-tests/src/web_sys.ts
Normal file
26
crates/typescript-tests/src/web_sys.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import * as wbg from "../pkg/typescript_tests";
|
||||
|
||||
const doc: Document = document;
|
||||
|
||||
const docStruct = wbg.DocStruct.new(doc);
|
||||
|
||||
const el: HTMLElement = document.createElement("a");
|
||||
|
||||
docStruct.append_element(el);
|
||||
docStruct.append_many(el, el, el);
|
||||
|
||||
const newDoc = docStruct.get_doc();
|
||||
|
||||
// This test ensures that the correct Typescript types are
|
||||
// used. If "newDoc" is "any", then "event" will cause the
|
||||
// compilation to fail because of noImplicitAny.
|
||||
newDoc.addEventListener("load", event => {
|
||||
console.log(event);
|
||||
});
|
||||
|
||||
// Same as above, but testing that the param is a document.
|
||||
const listener: Parameters<
|
||||
Parameters<typeof wbg["DocStruct"]["new"]>[0]["addEventListener"]
|
||||
>[1] = event => console.log(event);
|
||||
|
||||
newDoc.addEventListener("load", listener);
|
Reference in New Issue
Block a user