-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for #[wasm_bindgen(typescript_custom_section)].
- Loading branch information
Showing
7 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
guide/src/reference/attributes/on-rust-exports/typescript_custom_section.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# `typescript_custom_section` | ||
|
||
When added to a `const` `&'static str`, it will append the contents of the | ||
string to the `.d.ts` file exported by `wasm-bindgen-cli` (when the | ||
`--typescript` flag is enabled). | ||
|
||
```rust | ||
#[wasm_bindgen(typescript_custom_section)] | ||
const TS_APPEND_CONTENT: &'static str = r#" | ||
export type Coords = { "latitude": number, "longitude": number, }; | ||
"#; | ||
``` | ||
|
||
The primary target for this feature is for code generation. For example, you | ||
can author a macro that allows you to export a TypeScript definition alongside | ||
the definition of a struct or Rust type. | ||
|
||
```rust | ||
#[derive(MyTypescriptExport)] | ||
struct Coords { | ||
latitude: u32, | ||
longitude: u32, | ||
} | ||
``` | ||
|
||
The proc_derive_macro "MyTypescriptExport" can export its own | ||
`#[wasm_bindgen(typescript_custom_section)]` section, which would then be | ||
picked up by wasm-bindgen-cli. This would be equivalent to the contents of | ||
the TS_APPEND_CONTENT string in the first example. | ||
|
||
This feature allows plain data objects to be typechecked in Rust and in | ||
TypeScript by outputing a type definition generated at compile time. |