Trying to learn TypeScript

So I’m trying to learn TypeScript. TypeScript is not necessarily difficult to learn, I think most of my stumbling blocks have to do with working with definition files. Sometimes they work, sometimes they don’t and sometimes I just can’t get my vim or Grunt build scripts to understand what I’m doing.

Not a love story

This is not a success story. This is like Empire Strikes Back, I’ve made progress, but I’ve pretty much had my ass handed to me. For me, when I try to learn something related to JavaScript, I look at how I can incorporate it with the ArcGIS API for JavaScript. There’s a pretty good demo and definition file in the jsapi-resources repo from Esri on github. The demo in there is pretty solid too. I looked over this demo in addition to what I learned trying to work my way through Mayhem recently and came up with this.

The party

Here is what I came up with. First off, let’s go over the main.ts file.

One thing to note, if you are new to TypeScript, is the use of import and require, which is very much like commonjs. TypeScript of course also allows you to do type checking at compile time, which I really like. It’s like another layer of confidence in my code. For getting started, I’m using the any type, which is kind of cheating. For the mapOptions, I would probably actually create a Type for it and use that.

The next module is my actual widget, which just adds the map.

This is the kind of stuff that was killing my in trying to create widgets in Dojo with TypeScript. In TypeScript, you can create a class and do inheritance. The issue is, you can’t do multiple inheritance and something just feels weird about JavaScript classes, but that’s another topic entirely. This guy worked out TypeScript/Dojo stuff, but I found that if I just use dojo/_base/declare as-is, things worked out. Also, I would get the dreaded Cannot find external modules error when I compile, but if I set noImplicitAny to true in my Gruntfile, my code would compile and everything would still work.

The shame

This isn’t ideal, but it’s still a good learning exercise. I’m hoping to pick some Esri brains and get more insight here. Maybe just deep-dive mind-meld with TypeScript for a few days and fully embrace it, it will seep in. I mostly get stuck on the compile side with integrating definition files. I’m sure I’ll post more about TypeScript in the future. I’m really interested in Dojo 2 and Mayhem, so really doing things in a TypeScripty way is on my todo list.