DEV Community

Cover image for TypeScript? But WHY...?
WizardofHause
WizardofHause

Posted on • Updated on

TypeScript? But WHY...?

What is TypeScript?

It seems like it’s on every job post, listed right after to Python. But… what is it? Well, I know that Java is NOT JavaScript, so does that mean that TypeScript is also decidedly NOT JavaScript in an annoyingly misleading way? I was happy to hear that NO! TypeScript is like combining Java AND JavaScript together! I know, sounds lame, and in a way it is, but in a good way…

TypeScript is basically the well, actuallyyyy version of JavaScript. In all the ways that JavaScript is free-spirited and liberal with it’s return values and vague with it’s error messages, TypeScript is the nerdy sibling that knows all the rules to the boardgames and is very eager to correct you when you’re wrong.

The extra cool thing is that all JavaScript is apparently valid TypeScript. The main difference is that in TypeScript, we take the data type assignments -- kinda similar to the syntax used in Java -- and use them to smooth out the adorable and often incorrect guessing game that JavaScript sometimes plays with our data types.

let name = "Frank";
let age = 25;
console.log(name + age) 
//=> "Frank25"
Enter fullscreen mode Exit fullscreen mode

☝️ This is technically valid JS, but wouldn't it be better if it threw an error message instead of doing that pretty much useless concatenation?

let hasOranges = true; 
hasOranges = 5; // <- A boolean to a number? GROSS! WEIRD!
Enter fullscreen mode Exit fullscreen mode

☝️ This is also valid JS, but has the potential to cause some pretty obnoxious problems. That's where TypeScript comes in handy.

TypeScript basically extends JavaScript and is intended to encourage us as developers to be more explicit about the data types that we’re throwing around in our code. That’s where the type in _Type_Script comes from.

For example, you can specify a type by annotating it during variable declaration:

let playerName: string = 'Kevin';

Here playerName is our variable, and we’re telling our program that playerName should only be assigned string values.

By using a : and then the type name (string), we tell Typescript that the variable that we’re initializing should always be a string.

Typescript is basically an add-on, or like a JavaScript expansion if you will, that puts a stronger emphasis on enforcing data type restrictions for variables, so that they cannot be changed.

But...why would we want to do this? JavaScript already works fine, right? Sure, you’re not wrong. Especially if you’re working on a solo project or within a smaller dev team. But when projects need to scale up, and as teams get larger and larger, TypeScript helps to nail down the data types that are expected in the code of the program or application. It also reduces the time and effort we would normally have to take to debug or make updates to our code, when the need inevitably arises. Nerds save the day, once again.

TL;DR

Typescript is really useful for working in teams. There’s less guesswork and it’s more-or-less easier to read.

And that's all for now! This post was intentionally short and sweet, since my last one was such a beast. I'll have more installments on my exploration into TypeScript as I work through the problems HERE. I'm already through the first 4, so stay tuned for more notes!

As always, if you have any questions, comments, or recommendations for edits, feel free to reach out! You can find me on LINKEDIN and I welcome any and all feedback.

Keep fightin' the good fight! ʕ•ᴥ•ʔ

Top comments (0)