mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-14 05:21:24 +00:00
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:
@ -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']
|
||||
|
@ -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;
|
||||
|
34
crates/typescript-tests/src/omit_definition.rs
Normal file
34
crates/typescript-tests/src/omit_definition.rs
Normal 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,
|
||||
}
|
26
crates/typescript-tests/src/omit_definition.ts
Normal file
26
crates/typescript-tests/src/omit_definition.ts
Normal 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`.");
|
Reference in New Issue
Block a user