pub type Any
// Convert MoonBit value to JS
let js_val : @core.Any = @core.any(42)
let js_str : @core.Any = @core.any("hello")
// Cast back to MoonBit type
let num : Int = js_val.cast()
let str : String = js_str.cast()pub type Nullable[T]
// JavaScript value that might be null
let maybe_null : @core.Nullable[Int] = get_nullable_value()
match maybe_null {
Null => @console.log("Got null")
NotNull(value) => @console.log(value)
}pub type Nullish[T]
// JavaScript value that might be null or undefined
let maybe_nullish : @core.Nullish[String] = get_nullish_value()
match maybe_nullish {
Nullish => @console.log("Got null or undefined")
NotNullish(value) => @console.log(value)
}pub type Promise[T]
// Create promises
let p = @core.Promise::resolve(42)
let q = @core.Promise::reject("error")
// Wait in async context
async fn example() -> Int {
let value = p.wait()
value
}// Cast between types without conversion
let js_num : @core.Any = get_js_value()
let num : Int = @core.identity(js_num)let js_val = @core.any(42)
let js_str = @core.any("hello")
let js_bool = @core.any(true)let js_val : @core.Any = @core.any(42)
let num : Int = js_val.cast() // Recommended// Check JavaScript type
@core.typeof_(value) // "number", "string", "boolean", etc.
// Check if value is instance of constructor
@core.instanceof_(value, constructor)
// Null/undefined checks
@core.is_null(value)
@core.is_undefined(value)
@core.is_nullish(value)
// Type checks
@core.is_number(value)
@core.is_string(value)
@core.is_boolean(value)
@core.is_object(value)
@core.is_array(value)
@core.is_function(value)// Get property
let name = obj._get("name")
// Set property
obj._set("name", @core.any("Alice"))
// Index access (bracket notation)
obj["key"] = @core.any(value)
let val = obj["key"]// Call method
let result = obj._call("toString", [])
let result = obj._call("method", [@core.any(arg1), @core.any(arg2)])
// Invoke function
let fn = get_function()
let result = fn._invoke([@core.any(1), @core.any(2)])// Create empty object
let obj = @core.new_object()
obj["name"] = @core.any("Alice")
obj["age"] = @core.any(30)
// Create empty array
let arr = @core.new_array()
// Create object with constructor
let date = @core.new(date_constructor, [@core.any(2025), @core.any(11), @core.any(6)])// 0 arguments
let js_fn = @core.from_fn0(fn() -> String { "hello" })
// 1 argument
let js_fn = @core.from_fn1(fn(x: Int) -> Int { x * 2 })
// 2 arguments
let js_fn = @core.from_fn2(fn(x: Int, y: Int) -> Int { x + y })
// 3 arguments
let js_fn = @core.from_fn3(fn(x: Int, y: Int, z: Int) -> Int { x + y + z })// Promisify async functions
let promise_fn = @core.promisify0(async fn() -> Int { 42 })
let promise_fn = @core.promisify1(async fn(x: Int) -> Int { x * 2 })
// Run async code
@core.run_async(async fn() {
let value = some_promise.wait()
@console.log(value)
})
// Suspend current async context
@core.suspend(fn(resolve, reject) {
// Async operation
resolve(@core.any(42))
})// Try-catch for JavaScript exceptions
@core.throwable(fn() {
potentially_throwing_code()
}) catch {
err => @console.log("Caught error")
}// Console log (for debugging)
@core.log("Debug message")
@core.log(some_value)fn[T : ToAny] set_value(obj : Any, key : String, value : T) -> Unit {
obj._set(key, ToAny::to_any(value))
}#external
pub type Any#external
pub type Nullable[T]#external
pub type Nullish[T]#external
pub type Promise[T]let results = Promise::all([
from_async(async fn() { fetch_user() }),
from_async(async fn() { fetch_posts() }),
]).wait()let results = Promise::allSettled([
from_async(async fn() { 1 }),
from_async(async fn() { fail("error") }),
]).wait()
// results[0].status == "fulfilled", results[0].value == Some(1)
// results[1].status == "rejected", results[1].reason != nulllet first_success = Promise::any([
from_async(async fn() { fetch_from_mirror1() }),
from_async(async fn() { fetch_from_mirror2() }),
]).wait()let p : Promise[Int] = Promise::new(async fn(resolve, _reject) {
let result = some_async_operation()
resolve(result)
})let first = Promise::race([
from_async(async fn() {
sleep(100)
"slow"
}),
from_async(async fn() {
sleep(10)
"fast"
}),
]).wait() // "fast"pub(all) struct PromiseResolvers[T] {
promise : Promise[T]
resolve : (T) -> Unit
reject : (Error) -> Unit
}#external
pub type TemplateStringsArrayinterface TemplateStringsArray extends ReadonlyArray<string> {
readonly raw: ReadonlyArray<string>;
}#external
pub type TypeOf[T]#external
pub type Union2[A, B]#external
pub type Union3[A, B, C]#external
pub type Union4[A, B, C, D]#external
pub type Union5[A, B, C, D, E]let p : Promise[Int] = from_async(async fn() {
let result = some_async_operation()
result
})async fn[T, E : Error] suspend(f : ((T) -> Unit, (E) -> Unit) -> Unit) -> T raise Efn[T] tag(tag_fn : (TemplateStringsArray, Array[Any]) -> T, template : String, args : Array[Any]) -> Tfn(TemplateStringsArray, Array[Any]) -> Ttype TagFn<T> = (strings: TemplateStringsArray, ...values: any[]) => T// Define a typed tag function
fn my_css(
strings : @core.TemplateStringsArray,
values : Array[@core.Any],
) -> String {
let mut result = strings[0]
for i, v in values {
result = result + v.to_string() + strings[i + 1]
}
result
}
// Call it
let result : String = tag(my_css, "color: ${@0}", [@core.any("red")])let result = throwable(() => undefined()._invoke([]))js bindings for builtins/web/node/deno/bun (browser APIs split to mizchi/js_browser)
Dependencies