-
-
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.
Add guide page for typescript attribute
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
guide/src/reference/attributes/on-rust-exports/typescript.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,43 @@ | ||
# `typescript` | ||
|
||
By default, Rust exports exposed to JavaScript will generate TypeScript definitions (unless `--no-typescript` is used). The `typescript` attribute can be used to toggle this behavior per function, enum, struct, or field. For example: | ||
|
||
```rust | ||
#[wasm_bindgen(typescript = false)] | ||
pub enum MyHiddenEnum { | ||
One, | ||
Two, | ||
Three | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct MyPoint { | ||
pub x: u32, | ||
|
||
#[wasm_bindgen(typescript = false)] | ||
pub y: u32, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl MyPoint { | ||
|
||
#[wasm_bindgen(typescript = false)] | ||
pub fn stringify(&self) -> String { | ||
format!("({}, {})", self.x, self.y) | ||
} | ||
} | ||
``` | ||
|
||
Will generate the following `.d.ts` file: | ||
|
||
```ts | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
export class MyPoint { | ||
free(): void; | ||
x: number; | ||
readonly y: number; | ||
} | ||
``` | ||
|
||
When combined with [the `typescript_custom_section` attribute](typescript_custom_section.html), this can be used to manually specify more specific function types instead of using the generated definitions. |