TypeScreeps: Why Typescript?

4
2579

This article is aimed at complete beginners or people frustrated with JavaScript, perhaps coming from other programming languages. Already sold on the idea? Skip directly to this article where I give instructions for getting started.

Why TypeScript?

When I first started learning JavaScript, one thing that I missed were types. Strict typing in your programming language helps you catch a lot of bugs. If you are accustomed to types, it helps you read and understand the code. In the hands of a good IDE, it can make refactoring a breeze. As your codebases becomes larger, it makes maintaining the code simpler.

I hadn’t been using JavaScript very long when I decided to try TypeScript. I thought it might be too soon to try TypeScript without first feeling adept in JavaScript, but it turns out that wasn’t true. Coming from an object-oriented background, TypeScript actually helped me understand and be better at JavaScript.

TS vs JS in the IDE

Let’s try a little game. Here is a function I wrote in JS to calculate melee damage for a creep. How many bugs can you spot?

function meleeDamage(creep) {
let totalDamage = 0;
for (let part of creep.body) {
if (part.name !== ATTACK) { continue; }
let partDamage = ATTACK;
if (part.boost) {
partDamage *= BOOSTS[part.name][part.boost][ATTACK];
}
totalDamage += partDamage;
}
}
let damage = meleeDamage("myCreep");
if (damage > 0) {
console.log("yikes!");
}

No doubt you caught at least a few, perhaps all of them. Let’s see how the TypeScript compiler does.

Autocomplete in TS

If you like a little handholding by your IDE, then you will like TypeScript. It won’t stop at hand-holding, it will tuck you in and kiss you on the forehead.

Alright, sign me up!

If you’d like to give TypeScript a spin, I’ve written an article that should get you up and running.

4 COMMENTS