JavaScript & TS Interop
Explicit typed #extern boundaries for browser, Node, Bun, and Deno APIs, Promise-to-Async bridging, exception-to-Fail mapping, and strict TypeScript .d.ts emission.
Typed foreign bindings (#extern)
Exp is not a superset of JavaScript syntax — instead, it interoperates cleanly with the entire
npm, browser, Node, Bun, and Deno ecosystem through explicit, typechecked
#extern(js, ...) declarations:
module: "node:fs/promises", name: "readFile"— emits a static ESMimportbinding.global: "fetch"— binds directly to a host global (fetch,crypto,Math, Web APIs).method: "text"— calls a method on the first parameter receiver (res.text()).
type FetchError
NetworkFailure(String)
BadStatus(Int)
end
type JsResponse
status: Int
ok: Bool
end
#extern(js, global: "fetch", async: true, throws: FetchError)
fn jsFetch(url: String) -> JsResponse ! {Async, Fail<FetchError>}
end
#extern(js, method: "text", async: true, throws: FetchError)
fn jsResponseText(res: JsResponse) -> String ! {Async, Fail<FetchError>}
end
export fn getText(url: String) -> String ! {Async, Fail<FetchError>}
res = jsFetch(url)
if not res.ok then fail(BadStatus(res.status)) end
jsResponseText(res)
endPromise bridging & exception boundaries
Every foreign boundary states its async and error contract explicitly, verified against the
declared Exp signature (E0055 if mismatched):
-
async: true: bridges a returned JSPromise<T>into Exp'sAsynceffect, emitting a directawaitin async JS output. -
throws: E: wraps the foreign call in a boundarytry/catchconverting thrown JS exceptions into typedFail<E>values that match directly inside namedfn Fail.raise(Ctor(msg))handlers (preserving constructor tags for single- and multi-variant error types). Withoutthrows, unexpected JS exceptions surface cleanly as defects and run all lexical LIFO cleanups.
export type FsError
MissingFile(String)
end
#extern(js, module: "node:fs/promises", name: "readFile", async: true, throws: FsError)
fn readFileUtf8(path: String, encoding: String) -> String ! {Async, Fail<FsError>}
end
export fn loadConfigOrDefault(path: String) -> String ! {Async}
handle readFileUtf8(path, "utf8") with
fn Fail.raise(MissingFile(_)) "{}" end
end
endES2022+ ESM, Source Map v3 & strict .d.mts / .d.ts
Every export declaration in a .exp module emits a clean ES2022+
.mjs module, accurate Source Map v3 mappings (.mjs.map), and matching
strict TypeScript .d.mts (NodeNext ESM) + .d.ts
declarations verified against tsc --noEmit --strict --moduleResolution NodeNext:
- Exact 64-bit
Intmarshaling: exported functions marshal Exp 64-bitIntvalues across the TS boundary as exactbigint(range-checked at entry with$inInt/$outInt), preventing silent IEEE-754 53-bit truncation. - Typed errors & collections: fallible exports emit discriminated
ExpResult<T, E>({ $ok: true, value } | { $ok: false, error });Map<K, V>marshals to standard JSReadonlyMap<K, V>; nested options emit losslessExpOption<T>.
// Consuming an Exp .mjs + .d.mts module under strict TypeScript NodeNext
// (tsc --noEmit --strict --moduleResolution NodeNext passes with 0 errors)
import { area, totalArea, type Shape } from "./dist/hello.mjs";
import { buildInvoice, makeCents, type LineItem } from "./dist/billing.mjs";
const shapes: readonly Shape[] = [
{ $tag: 0, radius: 3.0 },
{ $tag: 1, w: 4.0, h: 4.0 },
];
const total: number = totalArea(shapes);
console.log(area(shapes[0]), total);
// 64-bit Exp Int exports marshal across the TS boundary as exact bigint
const lines: readonly LineItem[] = [
{ qty: 2n, sku: "PRO", unitPrice: makeCents(1500n) },
];
const res = buildInvoice(101n, "Acme", { $tag: 0, cents: 500n }, lines, new Map());
if (res.$ok) {
console.log(res.value.subtotal); // bigint (exact 64-bit Int)
} Engine-friendly runtime representation
The JavaScript backend enforces strict engine-friendliness invariants (EXP_STYLE.md §12.5):
- Monomorphic record shapes: fields of every canonical row type are emitted in identical sorted property order so V8 hidden classes and JSC structures stay monomorphic.
- Compact integer variant tags: algebraic variants use
$tag: 0, 1, ...;distincttypes erase completely to their underlying value with zero wrapper allocation. - Zero deopt patterns: emitted JS never contains
eval,with,Proxy,arguments,deleteon records, or prototype mutation.