DEV Community

loading...

Typescript and Google Apps Script

Bruce Axtens
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Updated on ・2 min read

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.

Discussion (0)