Are your cats old enough to learn about Typescript?

This was too long for a tweet (and I don't tweet anyway).

Helping build a modern web project with a nonprofit over the past few months opened my eyes to the world of Typescript. If you haven't yet heard Typescript is a superset of Javascript, adding support for gradually typed strong types.

Thinking in terms of a gradually typed language was new to me. The capabilities and behaviour of the compiler were (expectedly) different from that of gcc. I found the following video helpful in understanding the concepts and relation to strongly typed programming.

(I think there is an updated version of this video, but I have not watched it)

But wait, you usually (infrequently) blog about Ethereum and Solidity and cheese, what’s with this post? Typescript is firmly in crypto developers tool set, it’s recently consistently rated highly in StackOverflow’s “what’s hot” list and is probably become the most praised language in crypto podcasts behind Rust.

Javascript is the most practical defacto way to interact with web3, and Typescript seems like a no-brainer upgrade, particularly when dealing with any project of reasonable scale and complexity. The beauty of gradual typing here means that critical code paths can be strongly typed, without necessitating or enforcing types on the entire codebase, after all it is “just” a compile time tool that targets a flavour of Javascript. Runtime type checking can be provided with io-ts, which gives even more peace of mind, particularly at program runtime boundaries.

Numerous projects in the blockchain space primarily focus on adding Typescript support when building their interfaces to their chain based smart contracts. Typechain is an effort by a team at Neufund (early Typescript advocates alongside 0x, Bloom and Nomic Labs) to add Typescript bindings to smart contract interactions.

These tools mostly follow the same pattern: Feed it your ABI.json and web3 (or similar) target and import the wrapped contract calls in ES6. You now have eslint/tslint and many benefits of this gradually typed paradigm.

Apart from adding sanity and enforcing scalable structure to your Javascript-everywhere project, Typescript may be applicable to directly writing smart contracts themselves (coming soon tm)! Introducing AssemblyScript.

Typescript <> AssemblyScript

AssemblyScript is a subset of Typescript that can be compiled to WebAssembly. The future of script execution on blockchain networks is unknown at present, with eth2.0 designs moving towards using the beacon chain as a data availability layer, creating a paradigm _almost_ agnostic to execution engines.

There has long been a movement towards using a deterministic subset of WebAssembly (“Wasm”), termed Ethereum-Wasm (“colloquially termed eWasm”), with some eWasm execution environment already functional, like Parity’s Ethereum client. Alongside the performance benefits of closer to the metal instruction types (goodbye hairy 256 bit performance drawbacks and EVM OP_JUMP’S), the surrounding tooling is thought to be the main benefit of adoption. Mozilla and other mature web companies have been working on this project for many years.

Near Protocol, Ethereum, are all toying with this idea and have created demo execution environments for AssemblyScript (with a lot of functionality mocked out).

Will AssemblyScript extend the Javascript everywhere paradigm? AssemblyScript seems like we’ve gone full circle with the naming for sure.

Also work checking out is Deno, a secure “successor-but-not-successor” to nodeJS which also uses Typescript.

Edit (Nov 2020): a subset of Typescript called Static Typescript, intended as a replacement for C in the microcontroller and other similar memory limited environments is now available.

Admirer of polynomials