Add skip_typescript attribute to prevent .d.ts emit (#2016)

* Add skip_typescript attribute to prevent .d.ts emit

* Add guide page for typescript attribute
This commit is contained in:
Joey Watts
2020-03-03 10:34:28 -05:00
committed by GitHub
parent 3f4acc453b
commit 7ffb5ed70c
12 changed files with 227 additions and 56 deletions

View File

@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
wasm-bindgen = { path = '../..' }
web-sys = { path = '../web-sys', features = [ 'HtmlElement', 'Node', 'Document' ] }
js-sys = { path = '../js-sys' }
[lib]
crate-type = ['cdylib']

View File

@ -1,5 +1,6 @@
pub mod custom_section;
pub mod getters_setters;
pub mod omit_definition;
pub mod opt_args_and_ret;
pub mod optional_fields;
pub mod simple_fn;

View File

@ -0,0 +1,34 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen(typescript_custom_section)]
const TYPE_GET_VALUE: &'static str =
"export function take_function(func: (x: number) => void): void;";
#[wasm_bindgen(skip_typescript)]
pub fn take_function(_: js_sys::Function) {}
#[wasm_bindgen]
pub struct MyExportedStruct {
#[wasm_bindgen(skip_typescript)]
pub field: bool,
}
#[wasm_bindgen]
impl MyExportedStruct {
#[wasm_bindgen(skip_typescript)]
pub fn method(&mut self) {
self.field = !self.field;
}
#[wasm_bindgen(skip_typescript)]
pub fn static_func() {
panic!("oh no!");
}
}
#[wasm_bindgen(skip_typescript)]
pub enum MyEnum {
One,
Two,
Three,
}

View File

@ -0,0 +1,26 @@
import * as wbg from '../pkg/typescript_tests';
wbg.take_function((value) => {
// `value` should be inferred as `number` because of the
// custom typescript section. If `typescript = false` does
// not prevent the generation of a signature that takes any
// function, then this will trigger a noImplicitAny error.
console.log(value);
});
declare function assert<T>(message: T): void;
type EnableIfEnum = "MyEnum" extends keyof typeof wbg ? never : string;
assert<EnableIfEnum>("`MyEnum` type should not be exported.");
type EnableIfStruct = "MyStruct" extends keyof typeof wbg ? never : string;
assert<EnableIfStruct>("`MyStruct` type should not be exported.");
type EnableIfField = "field" extends keyof wbg.MyExportedStruct ? never : string;
assert<EnableIfField>("`field` should not exist on `MyExportedStruct`.");
type EnableIfMethod = "method" extends keyof wbg.MyExportedStruct ? never : string;
assert<EnableIfMethod>("`method` should not exist on `MyExportedStruct`.");
type EnableIfStaticMethod = "static_func" extends keyof typeof wbg.MyExportedStruct ? never : string;
assert<EnableIfStaticMethod>("`static_func` should not exist on `MyExportedStruct`.");