Skip to content

Commit

Permalink
Added inspectable generation to .d.ts file (#3000) (#3001)
Browse files Browse the repository at this point in the history
* Added inspectable generation to .d.ts file (#3000)

* Add ts tests (#3000)
  • Loading branch information
petrstudynka authored Jul 19, 2022
1 parent 1790a28 commit a8e934d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,16 @@ impl<'a> Context<'a> {
format!("{}{name}: this.{name},\n", fields, name = field_name)
})
));
// Also add definitions to the .d.ts file.
ts_dst.push_str(
"\
/**\n*\
* Return copy of self without private attributes.\n\
*/\n toJSON(): Object;\n\
/**\n\
* Return stringified version of self.\n\
*/\n toString(): string;\n",
);

if self.config.mode.nodejs() {
// `util.inspect` must be imported in Node.js to define [inspect.custom]
Expand Down
15 changes: 15 additions & 0 deletions crates/typescript-tests/src/inspectable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen(inspectable)]
pub struct Bar {
pub foo: i32,
_private: i32,
}

#[wasm_bindgen]
impl Bar {
#[wasm_bindgen(constructor)]
pub fn new(foo: i32) -> Self {
Self { foo, _private: 13 }
}
}
6 changes: 6 additions & 0 deletions crates/typescript-tests/src/inspectable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as wbg from "../pkg/typescript_tests";

const bar: wbg.Bar = new wbg.Bar(1);
const barAsJson: Object = bar.toJSON();
const barAsString: string = bar.toString();
const _ = bar.foo;
1 change: 1 addition & 0 deletions crates/typescript-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod custom_section;
pub mod getters_setters;
pub mod inspectable;
pub mod omit_definition;
pub mod opt_args_and_ret;
pub mod optional_fields;
Expand Down

0 comments on commit a8e934d

Please sign in to comment.