Modal Title
Cloud Native Ecosystem / Open Source / Software Development

How TypeScript Can Speed up Your Adoption of WebAssembly

AssemblyScript, a variant of TypeScript, can help eliminate some hurdles for web developers who want to start using WebAssembly.
Jan 14th, 2022 5:00am by
Featued image for: How TypeScript Can Speed up Your Adoption of WebAssembly

WebAssembly, also known as WASM, is being touted as one of the top cloud native trends to watch out for in 2022. WASM — a fast, secure and powerful way to run code across a variety of platforms — bears an uncanny resemblance to container runtimes. A number of projects and startups are working on accelerating its adoption.

The technology is relatively new — it was first released just under five years ago. So there’s still a lot of work being done to improve the toolchain setup, as Fintan Ryan, a senior analyst at Gartner, noted in a previous New Stack article.

The newness of WASM, and the rapid innovation around it could feel daunting to those from the web developer background, However, knowledge of TypeScript, a strongly typed language building on JavaScript, might just make taking those first steps a little less intimidating.

The Complexities of WebAssembly

JavaScript wasn’t designed to be a compilation target for the web. Owing to its ubiquity and lower entry-level barrier for adoption, it ended up becoming one. But JavaScript brings with it some pitfalls.

Although portable and fast, the performance of JavaScript was unpredictable when it came to complex web applications. This paved the way for efforts to design a compilation target for the web that was fast, secure, portable and enabled high performance — and for standardizing those efforts.

Enter WebAssembly. With a bytecode format and an associated text code format that could be used by virtually every browser irrespective of the platform as a compilation target, it won on almost all these fronts.

But one look at the compiled code was enough to intimidate programmers, because it lacked all the high-level abstractions that a programming language offered. While its purpose was not to be manually written, since it was designed to be a compilation target, adopters did need to learn one of the languages that could compile to WASM.

While compilers were developed for typed languages like C, C++, and Rust, those without a type system like JavaScript were left behind, owing to the fact that WASM itself was statically typed.

It was only in 2017 that AssemblyScript made its way into the ecosystem and eliminated the need for web devs to learn a new language altogether if they wanted to harness the benefits of WASM.

What Is AssemblyScript?

A variant of TypeScript, AssemblyScript targets the WASM feature set and allows programmers to have low-level control over their code. Like TypeScript, AssemblyScript is also open source.

The documentation for AssemblyScript states, “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.”

Unlike TypeScript that compiles to JavaScript, AssemblyScript is compiled to WebAssembly directly by Binaryen, correlating to higher speed and performance.

How Does AssemblyScript Help?

If JavaScript is your preferred programming language, TypeScript is pretty much its superset, with an addition of optional static typing while remaining syntactically similar. AssemblyScript, as mentioned previously, is a stricter variant of TypeScript which means that there isn’t a requirement to start from scratch and adopters are therefore able to build upon existing knowledge.

“TypeScript, the statically typed variant of JavaScript, is a widely adopted and easy to understand type safety system for web apps,” Connor Hicks, founder and CEO of the serverless engine company Suborbital, told The New Stack.

“Since WebAssembly is very strongly typed, it made a lot of sense to create a language using TypeScript syntax for web developers to easily build with WASM,” Hicks said. “AssemblyScript is one of the easiest ways for developers to use a familiar language while getting the performance and portability benefits of WebAssembly”

However, even though it bears some similarities to TypeScript, AssemblyScript should not be mistaken as a subset of it. Behavioral and feature-specific surprises need to be accounted for because, after all, they are compiling to two very different targets.

A quick glance at the documentation is enough to understand that the features making JavaScript (and TypeScript) a favorite among web devs have been stripped off when it comes to the development of AssemblyScript.

Whether that means the absence of common operators like any, void, and undefined or of DOM access due to WebAssembly modules running in sandboxes, there is definitely a lot of cultural and mindset shift that accompanies the adoption of AssemblyScript (and WebAssembly).

With greater maturity, some important features of TypeScript, like closure support for all functions, will likely get incorporated into AssemblyScript. However, one must remember that while familiarity with TypeScript could be a stepping stone towards WebAssembly adoption via AssemblyScript, keep in mind that AssemblyScript is not an exact subset of TypeScript.

Because it was tailor-made for WebAssembly, AssemblyScript’s evolution will be tied to the ways in which WebAssembly specifications develop. If you know Typescript, there’s much to build upon in the syntactic similarities between that and AssemblyScript — but the latter is writing a brand new story with elements of a familiar language.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.