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
Atm, because of rust orphan rule we cant directly implement IntoJsResult trait for a struct/type wrapper in Result<> and this issue prevents us to use typed returns on async functions as:
// `A` and `Error` both have all needed trait implementations manualy except `IntoJsResult` for a wrapped `Result<>` of them#[derive(Serialize,Deserialize,Tsify)]#[tsify(into_wasm_abi, from_wasm_abi)]structA;// notice there is no #[wasm_bindgen] used for this struct as we just wanna get a typed obj in js/ts and not a class obj#[wasm_bindgen]pubfnsome_function() -> Result<A,Error>{/// this compiles ok and works ok};#[wasm_bindgen]pubasyncfnsome_function() -> Result<A,Error>{/// this doesnt compile with error complaining about IntoJsResult not implemented for Result<A, Error>};
Same situation for wrapped type of T for ReturnWasmAbi:
pubtypeB = Result<A,Error>#[wasm_bindgen]pubfnsome_function() -> Result<B,Error>{/// this doesnt compile with error complaining about ReturnWasmAbi not implemented for Result<A, Error>/// and same error for async fn along with IntoJsResult error};
Proposed Solution
Prefered solution (my personal choice) can be to allow custom ts return type specified like other wasm_bindgen artts like js_name, where it would apply that to the return type of the typescript generated output for an standalone fn or a class method, because this issue also effects class methods where you cant return a typed type for a single method and it ends up being any, and if you wanna override that, you'd needing to override the whole typescript of the class and all its methods using custom typescript section, instead of just being able to override that single class method. Probably this solution is easiest of the two to implement, as it would just affect the generated .d.ts as the generated js cant really type the return type of a function with a typescript defined obj type.
So this solution can be adopted to provide typings for return types of any function and method for generated typescript for anything that returns JsValue (any in js) allowing users to fully type their output pkgs, ie functions/methods can return JsValue on rust side and handling conversion in their rust body, and still generated ts output can have a fully typed return type
#[derive(Serialize,Deserialize,Tsify)]#[tsify(into_wasm_abi, from_wasm_abi)]structSomeCustomType;// we also manually imple other wasm traits such as LongRefWasmAbi, etc without using #[wasm_bindgen]#[wasm_bindgen(js_name = "someFn", typescript_return_type = "SomeCustomType")]pubfnsome_fn() -> JsValue{// fn body}#[wasm_bindgen]structA;#[wasm_bindgen]implA{#[wasm_bindgen(js_name = "someMethod", typescript_return_type = "Promise<SomeCustomType>")]pubasyncfnsome_method() -> Result<JsValue,Error>{// method body}}
Another solution is to provide some macro or wrapper trait where IntoJsResult trait can be implemented for types that dont necessarily use #[wasm_bindgen] on their declaration but implement wasm traits manualy, so that they can be returned from async function as well. Note that this solution most probably cant handle every usecase and situations.
that would be:
// need some way to be able to implement this for a type individually just like any other wasm trait// ideally need this to be impl the same for wrapped types of T such as Vec<T>, Option<T>, etc as wellimpl<T:Into<JsValue>,E:Into<JsValue>>IntoJsResultforResult<T,E>
same goes for ReturnWasmAbi for wrapped types of <T, E>.
Alternatives
Atm the only workaround for this that I know of is to use custom typescript section to handcode the typescript for the given fn, so its return type is typed on the final .d.ts output
The text was updated successfully, but these errors were encountered:
rouzwelt
changed the title
allow manual/standalone implementation for IntoJsResult for types without using #[wasm_bindgen]
allow manual/standalone implementation for IntoJsResult for types and ReturnWasmAbi for wrapped types without using #[wasm_bindgen]
Dec 25, 2024
rouzwelt
changed the title
allow manual/standalone implementation for IntoJsResult for types and ReturnWasmAbi for wrapped types without using #[wasm_bindgen]
allow specifying return type for functions/methods when the rust side is JsValue
Dec 25, 2024
Motivation
Atm, because of rust orphan rule we cant directly implement
IntoJsResult
trait for a struct/type wrapper in Result<> and this issue prevents us to use typed returns on async functions as:Same situation for wrapped type of T for
ReturnWasmAbi
:Proposed Solution
js_name
, where it would apply that to the return type of the typescript generated output for an standalone fn or a class method, because this issue also effects class methods where you cant return a typed type for a single method and it ends up beingany
, and if you wanna override that, you'd needing to override the whole typescript of the class and all its methods using custom typescript section, instead of just being able to override that single class method. Probably this solution is easiest of the two to implement, as it would just affect the generated .d.ts as the generated js cant really type the return type of a function with a typescript defined obj type.So this solution can be adopted to provide typings for return types of any function and method for generated typescript for anything that returns JsValue (
any
in js) allowing users to fully type their output pkgs, ie functions/methods can return JsValue on rust side and handling conversion in their rust body, and still generated ts output can have a fully typed return typeIntoJsResult
trait can be implemented for types that dont necessarily use #[wasm_bindgen] on their declaration but implement wasm traits manualy, so that they can be returned from async function as well. Note that this solution most probably cant handle every usecase and situations.that would be:
same goes for
ReturnWasmAbi
for wrapped types of <T, E>.Alternatives
Atm the only workaround for this that I know of is to use custom typescript section to handcode the typescript for the given fn, so its return type is typed on the final .d.ts output
The text was updated successfully, but these errors were encountered: