Exp 0.1
Tooling & Backends

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 ESM import binding.
  • 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)
end
Hover or focus any symbol to inspect inferred types, effects, and errors

Promise 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 JS Promise<T> into Exp's Async effect, emitting a direct await in async JS output.
  • throws: E: wraps the foreign call in a boundary try/catch converting thrown JS exceptions into typed Fail<E> values that match directly inside named fn Fail.raise(Ctor(msg)) handlers (preserving constructor tags for single- and multi-variant error types). Without throws, 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
end
Hover or focus any symbol to inspect inferred types, effects, and errors

ES2022+ 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 Int marshaling: exported functions marshal Exp 64-bit Int values across the TS boundary as exact bigint (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 JS ReadonlyMap<K, V>; nested options emit lossless ExpOption<T>.
consumer.mts (NodeNext) ts
// 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, ...; distinct types erase completely to their underlying value with zero wrapper allocation.
  • Zero deopt patterns: emitted JS never contains eval, with, Proxy, arguments, delete on records, or prototype mutation.