dioxus-use-js
A macro that generates Rust bindings to JavaScript or TypeScript functions, with compile time checks. For use with Dioxus. No need to use eval directly anymore!
Works across all eval supported platforms (Web, Desktop, Mobile, and liveview) — no wasm-bindgen required.
JavaScript Usage
You can write plain JavaScript and bind exported functions directly.
// assets/example.js
export
Bind it in Rust:
use use_js;
use_js!;
Generated Rust signature:
async ;
Use it like:
let val: String = greeting.await?;
TypeScript Usage
If you use TypeScript, the macro will parse types to produce more accurate Rust bindings. See the Type Mapping section for details on how TypeScript types are mapped to Rust types.
// js-utils/example.ts
export function greeting(from: string, to: string): string {
return `Hello ${to}, this is ${from} speaking from JavaScript!`;
}
Compile with:
See BunTsCompile for use in
build.rsas well - Example
Bind with:
use_js!;
Generated Rust signature:
async ;
Macro Syntax
Js
use_js!;
use_js!;
use_js!;
Ts
use_js!;
use_js!;
use_js!;
Type Mapping
Built-in TypeScript Types
| TypeScript | Rust Input | Rust Output |
|---|---|---|
string |
&str |
String |
number |
f64 |
f64 |
boolean |
bool |
bool |
T | null |
Option<&T> |
Option<T> |
T[] |
&[T] |
Vec<T> |
Map<T, TT> |
&HashMap<T, TT> |
HashMap<T, TT> |
Set<T> |
&HashSet<T> |
HashSet<T> |
void, undefined, never, null |
- |
() |
any, unknown, object, -, * |
impl serde::Serialize |
T: serde::de::DeserializeOwned |
Promise<T> |
&T |
T |
Special Types
| TypeScript | Rust Input | Rust Output |
|---|---|---|
Json |
&serde_json::Value |
serde_json::Value |
JsValue<T>, JsValue |
&JsValue |
JsValue |
RustCallback<T,TT> |
impl AsyncFnMut(T) -> Result<TT, Box<dyn Error + Send + Sync>> |
- |
RustCallback<void,TT> |
impl AsyncFnMut() -> Result<TT, Box<dyn Error + Send + Sync>> |
- |
Special Types
Special types are types not included in the regular Typescript type system, but are understood by the use_js! macro and may augment the generated binding code.
Json
Json is a simple type that represents valid json. This type can best nested.
type Json = string | number | boolean | null | { [key: string]: Json } | Json[];
Example Usage
TypeScript:
type Json = string | number | boolean | null | { [key: string]: Json } | Json[];
export function json(): Json[] {
return [
{"key": "value"},
{"key": "value"},
];
}
Generated Rust signature:
pub async ;
JsValue: Javascript References
This special TypeScript type signals to the macro to bypass serialization and pass native JS values as opaque references between Rust and JavaScript. The macro generates the glue code required. The JS value is automatically disposed when all references on the Rust side go out of scope. Only the following are valid representations:
| Valid Ts Uses | Input | Output |
|---|---|---|
JsValue<T>, JsValue |
&JsValue |
JsValue |
Promise<JsValue<T>>, Promise<JsValue> |
- |
JsValue |
JsValue<T> | null JsValue | null |
Option<&JsValue> |
Option<JsValue> |
Promise<JsValue<T> | null>, Promise<JsValue | null> |
- |
Option<JsValue> |
type JsValue<T = any> = T;
Example Usage
TypeScript:
type JsValue<T = any> = T;
type MyObject = {
name: string;
method: (value: number) => number;
};
export function createJsObject(): JsValue<MyObject> {
return {
name: "example",
method: function (value) {
return value + 25;
},
};
}
export function useJsObject(value: JsValue<MyObject>): number {
let result = value.method(2);
return result;
}
Generated Rust signature:
pub async ;
pub async ;
Usage:
let js_value_example: = use_resource;
RustCallback: Passing Closures from Rust to JavaScript
This special TypeScript type signals to the macro that a Rust async closure will be passed into the JavaScript function. The macro generates the glue code required. This enables advanced interop patterns, such as calling Rust logic from within JS — all while preserving type safety. This type cannot be nested.
type RustCallback<A, R> = (arg: A) => Promise<R>;
If input A is void then the Rust closure will take no input.
If output R is void then the Rust closure will return no output.
Example Usage
TypeScript:
type RustCallback<A, R> = (arg: A) => Promise<R>;
export async function useCallback(
startingValue: number,
callback: RustCallback<number, number>
): Promise<number> {
let doubledValue = startingValue * 2;
let quadValue = await callback(doubledValue); // Calls back into Rust
return quadValue * 2;
}
Generated Rust signature:
pub async ;
Usage:
let callback_example: = use_resource;