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:
Bennett Hardwick
2020-02-20 01:14:32 +10:00
committed by GitHub
parent 9d55978af5
commit ec1b9453c9
14 changed files with 140 additions and 6 deletions

View File

@ -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;

View 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) {}
}

View 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);