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 Complexities of WebAssembly
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.
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.
How Does AssemblyScript Help?
“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.
Whether that means the absence of common operators like
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.