Skip to main content

WebAssembly with TypeScript

note
  • TypeScript is a superset of JavaScript. Therefore, the best approach to using TypeScript with WebAssembly is to transpile it into JavaScript.
  • WebAssembly support for JavaScript is good thanks to Javy.
  • Javy compiles QuickJS, a tiny JavaScript runtime, to Wasm along with the script to be executed.
  • JavaScript is the second most popular and desirable language to work with WebAssembly.

Install TypeScript compiler​

If you have npm, you can install TypeScript globally:

npm install -g typescript

TypeScript code​

Create a file named index.ts:

function fibonacci(num: number) {
var a: number = 1;
var b: number = 0;
var temp: number;
while (num >= 0) {
temp = a;
a = a + b;
b = temp;
num--;
}
console.log("Fibonacci Term is:", b);
}
tip

Access the TypeScript codex repository for code samples, including the fibonacci example.

Transpiling TypeScript to JavaScript​

tsc index.ts

Follow the instructions at WebAssembly with JavaScript to compile JavaScript to WebAssembly and run it with Enarx.