Typescript - Assembly Script

  • A TypeScript-like language for WebAssembly

    Introduction

    AssemblyScript compiles a variant of TypeScript (a typed superset of JavaScript) to WebAssembly using Binaryen.

    Good use cases for AssemblyScript are computation-heavy logic like image manipulation, hot game logic, specialized algorithms, emulators, compilers, and the likes.

    Project

    Create a new project with Deno

    Terminal window
    deno init assembly
    Terminal window
    npm init -y
    npm install --save-dev assemblyscript
    npx asinit .

    This creates assembly/ with index.ts and asconfig.json, plus build scripts in package.json.

    Build the WebAssembly module

    Terminal window
    npm run asbuild # or: npx asc assembly/index.ts --target release

    Output will be in build/ (e.g., build/release.wasm).

    Import and run the WASM in Deno

    Create main.ts:

    const wasmUrl = new URL("./build/release.wasm", import.meta.url);
    const wasmModule = await WebAssembly.instantiate(
    await Deno.readFile(wasmUrl),
    {}, // import object if needed
    );
    const { add } = wasmModule.instance.exports as { add(a: number, b: number): number };
    console.log(add(2, 3)); // expect 5

    Run:

    Terminal window
    deno run --allow-read main.ts

    Develop your AssemblyScript code

    • Edit assembly/index.ts. Export functions you need in JS/TS:
    export function add(a: i32, b: i32): i32 {
    return a + b;
    }

    Rebuild after changes: npm run asbuild.

    Optional Deno config

    If you want TypeScript pathing or permissions presets, create deno.json:

    {
    "tasks": {
    "start": "deno run --allow-read main.ts"
    }
    }

    Then run: deno task start.

    Debugging tips

    • Use npm run asbuild:debug (generated by asinit) for better stack traces.
    • Check asconfig.json targets to tweak optimization levels or exports.

    That’s it—modify assembly/index.ts, rebuild, and run via Deno.

    Resources