You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support optional fields in struct reflect in typescript, for example:
#[wasn_bindgen]pubstructPing{pong:Option<bool>,}
in .d.ts
exportclassPing{pong?: bool;}
Proposed Solution
Struct generation in wasm_bindgen separate fields into getters and setters, and the ret_ty for getter or args for setter which decide the final field type out are embedded in JsBuilder, we can't checkout if some fields' types are optional, but...optional fields generate some special docs like @returns{{{}}} and @params{{{}}} we can trace.
If it is appropriate to render fields' types with checking if its doc contains .. | undefined ?
I like the idea of generating optional fields... however, I would suggest that whatever tool you are using should work with | undefined, because from JS's perspective there isn't much difference, and many TypeScript programs will use | undefined.
The | undefined checking just applies to setter and getter in .d.ts, if this solution can make a pr, I'd like to do it, or any other way can add optional fields definition to Typescript...really need this.
btw, @Pauan, would you mind to have a look at #1986, I just wrote a pr about #[wasm_bindgen(plain_object)] yesterday.
The words in your avatar are really awesome! “花鳥" means flowers and birds in Chinese and "旅鳥" means a bird keep travelling!
Motivation
Support optional fields in struct reflect in typescript, for example:
in
.d.ts
Proposed Solution
Struct generation in
wasm_bindgen
separate fields intogetters
andsetters
, and theret_ty
forgetter
orargs
forsetter
which decide the final field type out are embedded inJsBuilder
, we can't checkout if some fields' types are optional, but...optional fields generate some special docs like@returns{{{}}}
and@params{{{}}}
we can trace.If it is appropriate to render fields' types with checking if its doc contains
.. | undefined
?For example, we can change this line
wasm-bindgen/crates/cli-support/src/js/mod.rs
Line 3070 in f507a2a
and this line
wasm-bindgen/crates/cli-support/src/js/mod.rs
Line 703 in f507a2a
Alternatives
I think the solution above is quite a mess, but I can't figure out a better one.
The text was updated successfully, but these errors were encountered: