Typesafe Truffle with Typescript and TypeChain

Kris Kaczor
Ethereum TS
Published in
2 min readSep 26, 2018

Truffle is de facto standard when it comes to developing Ethereum smart contracts in Javascript. Unfortunately, currently, it doesn’t work with Typescript. Creating typings for Truffle API is not the only problem, the real challenge is creating typings for smart contracts under development.

Last year we’ve presented Typechain — a way to generate dynamic typings for smart contracts based on their ABIs. Back then we’ve used Web3 0.20.x and developed custom wrappers build on top. Now if you use Truffle we got you covered!

Truffle meets Typechain

Typechain changed in a great way since the original announcement. Almost 300 Github ⭐ laters, we have support for multiple targets, including Truffle! Now it’s as easy as:

typechain — target truffle ./build/**/*.json

We have also developed typings to “static” parts of Truffle API.

The whole setup is described in example repo that we’ve prepared.

Okay, okay but why?

If you’re not a Typescript user, here’s a short list of benefits of using TypeChain.

First of all you, you will get full IDE support while writing test, meaning your editor will be able to autocomplete methods on smart contract as well as aid with parameter passing.

Me looking at test source code and back at smart contract interface

Types will also help you find any breaking changes in your smart contracts API during migration. Another great benefit is the ability to run a more complicated static analysis, for example by using tslint ruleset TypeStrict you’re able to find floating promises in your code. Promises, if not handled properly could result in tests that are passing when in reality they should fail.

Summary

Typescript popularity is exploding across Javascript developers and now we have an easy way to use it in the most popular smart contract development framework. TypeChain will speed up your development speed and make your tests harder to break.

Happy coding! 🔥🔥🔥

PS: TypeChain became part of the newly created organization — EthereumTS. Our mission is a development of high-quality tools, written in Typescript for Ethereum ecosystem. Follow us on Twitter, Github, and Medium!

--

--