typescript_custom_section
當添加到 const
&'static str
時,它會將字串的內容附加到 wasm-bindgen-cli
導出的 .d.ts
檔案(當啟用 --typescript
標誌時)。
#![allow(unused)] fn main() { #[wasm_bindgen(typescript_custom_section)] const TS_APPEND_CONTENT: &'static str = r#" export type Coords = { "latitude": number, "longitude": number, }; "#; }
此功能的主要目標是用於程式碼產生。例如,您可以撰寫一個巨集,讓您能夠在結構或 Rust 類型的定義旁邊導出 TypeScript 定義。
#![allow(unused)] fn main() { #[derive(MyTypescriptExport)] struct Coords { latitude: u32, longitude: u32, } }
proc_derive_macro "MyTypescriptExport" 可以導出自己的 #[wasm_bindgen(typescript_custom_section)]
區段,然後該區段會被 wasm-bindgen-cli 擷取。這相當於第一個範例中 TS_APPEND_CONTENT 字串的內容。
此功能允許在 Rust 和 TypeScript 中對純資料物件進行類型檢查,方法是輸出在編譯時產生的類型定義。