DEV Community

Bruce Axtens
Bruce Axtens

Posted on • Updated on

Typescript and Google Apps Script

Today I learned how to use Typescript. I've got a steadily growing pair of Google Apps Script (GAS) projects and Typescript helped me catch some nasty surprises before they jumped out at me.

It was a simple process too: install the npm modules and set up a couple of configuration files. After that I renamed all my .js to .ts and started putting in type annotations. Pretty soon the "Problems" tab in VSCode started to fill up with notifications. After they were fixed I pushed the code up to Google Sheets and had a look in the online script editor, half expecting a mess. But no mess, just clean ES3 JavaScript.

I was also pleasantly surprised by ts2gas -- code that interposes itself between VSCode and Google Sheets in the clasp pipeline. ts2gas is a "function that transpiles TypeScript to Google Apps Script" and does an exceptional job of it too, including taking ES6 (including classes, let, const and all that) and converting it to the ES3 that GAS (currently) uses.

In my previous postings about javascript I've used Babel to do the conversion to ES3 (for those still stuck on that planet.) Looks like I'll be typescripting and ts2gas-ing them too.

Do be careful with ts2gas. I just bumped into an issue with it's conversion which I have just logged. I've also got a StackOverflow question on a related issue.

Top comments (0)