TypeScript? But WHY...?
Tags: typescript, beginners, programming, codenewbie
Published: 2023-04-28 23:58:37

## 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. 


<img width="100%" style="width: 100%" src="https://media.giphy.com/media/26xBEvN97V0zPnx3G/giphy.gif">


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"
```
☝️ 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!
```
☝️ 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. 

<img width="100%" style="width: 100%" src="https://media.giphy.com/media/87jGhdRVzUOJNh2s0q/giphy.gif">

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.

<img width="100%" style="width: 100%" src="https://media.giphy.com/media/ToMjGpRhf96j23aTc5i/giphy.gif">

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](https://typescript-exercises.github.io/). 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](https://www.linkedin.com/in/jon-hause/) and I welcome any and all feedback.

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