AssemblyScript - TypeScript to Wasm Compiler

Mar 02, 2020 • ☕️ 2 min read

AssemblyScript is a compiler which architecture differs from a JavaScript VM in that it compiles a program ahead of time, quite similar to other static compilers. One can think of it as a mix of TypeScript’s syntax and C’s capabilities.

On top of WebAssembly types, it not only provides low-level built-ins to access WebAssembly features directly, making it suitable as a thin layer on top of Wasm, but also comes with its own JavaScript-like standard library, making it suitable for non-browser use cases, along a relatively small managed runtime (with memory management and garbage collection) enabling the creation of programs that look and feel much like TypeScript.

The compiler still has its limitations and we are waiting for WebAssembly features that are currently undergoing specification.

WebAssembly is fundamentally different from JavaScript, ultimately enabling entirely new use cases not only on the web. Consequently, AssemblyScript is much more similar to a static compiler than it is to a JavaScript VM. One can think of it as if TypeScript and C had a somewhat special child.

Unlike TypeScript, which targets a JavaScript environment with all of its dynamic features, AssemblyScript targets WebAssembly with all of its static guarantees, hence intentionally avoids the dynamicness of JavaScript where it cannot be compiled ahead of time efficiently.

npm install --save-dev assemblyscript

Similar to TypeScript’s tsc compiling to JavaScript, AssemblyScript’s asc compiles to WebAssembly. Let’s create our hello-world.ts AssemblyScript file:

// This exports an add function.
// It takes in two 32-bit integer values
// And returns a 32-bit integer value.
export function add(a: i32, b: i32): i32 {
  return a + b

Then, let’s compile that into a wasm module, using the AssemblyScript Compiler, which will output a hello-world.wasm:

asc hello-world.ts -b hello-world.wasm

This is great for TypeScript and JavaScript developers who want to write WebAssembly, without learning a new language.

Reading More

The Opinionated Decision on CSS Resets

On debate of different strategies to fix inconsistent default styles across browsers using Reset, Normalize or Custom CSS

Become a Fullstack JavaScript Developer, Part 7: The Mobile

An overview look at current mobile development landscape including 4 major platforms

Type Safety in JavaScript

Static type checkers and object validators are just incomplete solutions to the unsafe dynamic nature of JavaScript