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.
You can write plain JavaScript and bind exported functions directly.
// example.js
export function greeting(from, to) {
return `Hello ${to}, this is ${from} speaking from JavaScript!`;
}
Bind it in Rust:
use dioxus_use_js::use_js;
use_js!("assets/example.js"::greeting);
Generated Rust signature:
async fn greeting<T: DeserializeOwned>(from: impl Serialize, to: impl Serialize) -> Result<T, JsError>;
Use it like:
let val: String = greeting("Alice", "Bob").await?;
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.
// example.ts
export function greeting(from: string, to: string): string {
return `Hello ${to}, this is ${from} speaking from JavaScript!`;
}
Compile with:
bun build src/example.ts --outfile assets/example.js
Bind with:
use_js!("src/example.ts", "assets/example.js"::{greeting});
Generated Rust signature:
async fn greeting(from: &str, to: &str) -> Result<String, JsError>;
use_js!("bundle.js"::function);
use_js!("bundle.js"::{func1, func2});
use_js!("bundle.js"::*);
use_js!("source.ts", "bundle.js"::function);
use_js!("source.ts", "bundle.js"::{func1, func2});
use_js!("source.ts", "bundle.js"::*);
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 |
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 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 is a simple type that represents valid json. This type can best nested.
type Json = string | number | boolean | null | { [key: string]: Json } | Json[];
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 fn json() -> Result<Vec<Value> ,JsError>;
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;
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 fn createJsObject() -> Result<JsValue, JsError>;
pub async fn useJsObject(value: &JsValue) -> Result<f64, JsError>;
Usage:
let js_value_example: Resource<Result<f64, JsError>> = use_resource(|| async move {
// No serialization!
// The value is kept on the js side and a reference to it is kept on the rust side.
// The value is automatically disposed when all rust references no longer exist.
let js_value = createJsObject().await?;
let output = useJsObject(&js_value).await?;
// Since `js_value` is dropped here and all references no longer exist,
// the referenced value will be disposed on the js side.
Ok(output)
});
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.
Note: On the Javascript side, within the same exported function invocation, do not call another callback before the previous callback has completed. Otherwise, the returned values from Rust may go to the wrong callback.
Bad:
let promise1 = callback(); let promise2 = callback();Good:
let value1 = await callback(); let value2 = await callback();The obvious exception to this is if none or only one of the callbacks in process return a value, then this is fine.
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 fn useCallback(
startingValue: f64,
mut callback: impl AsyncFnMut(f64) -> Result<f64, Box<dyn Error + Send + Sync>>,
) -> Result<f64, JsError>;
Usage:
let callback_example: Resource<Result<f64, JsError>> = use_resource(|| async move {
// Rust async closure that will be called by JS
let callback = async |value: f64| Ok(value * 2.0);
// Pass it into the JS function
let value = useCallback(2.0, callback).await?;
Ok(value)
});