AssemblyScript
AssemblyScript is a TypeScript-like language designed specifically for Wasm. It has static types (i32, f64, u8...), compiles via Binaryen, produces small output, and has a familiar syntax for web developers.
Features: generics, classes, closures (limited), memory management runtimes (incremental GC, minimal, stub), SIMD (v128), load<T>/store<T> intrinsics, @inline, unchecked().
Differences from TypeScript: no any/union types (except nullable), no dynamic objects, integer types matter, and a limited stdlib.
npm init -y
npm install --save-dev assemblyscript
npx asinit .// assembly/index.ts
export function add(a: i32, b: i32): i32 {
return a + b;
}
export function fib(n: i32): i64 {
let a: i64 = 0, b: i64 = 1;
for (let i = 0; i < n; i++) {
const t = a + b;
a = b;
b = t;
}
return a;
}
export function sumArray(arr: Float64Array): f64 {
let total = 0.0;
for (let i = 0; i < arr.length; i++) total += unchecked(arr[i]);
return total;
}
export function greet(name: string): string {
return "Hello, " + name + "!";
}
@external("env", "logValue")
declare function logValue(v: f64): void;
export class Vec2 {
constructor(public x: f64, public y: f64) {}
length(): f64 { return Math.sqrt(this.x * this.x + this.y * this.y); }
}npm run asbuild # builds build/debug.wasm and build/release.wasm + JS bindings (--bindings esm)import { add, greet, sumArray } from "./build/release.js";
console.log(add(1, 2), greet("Ana"), sumArray(new Float64Array([1, 2, 3])));