Getting started with TypeScript as a JavaScript developer
Tags: typescript, javascript, react
Published: 2020-12-01 21:22:41

If you're a JavaScript developer, you might have heard of TypeScript already.

Or maybe you've had a quick dip into it, but the merit isn't immediately obvious.

Or perhaps your colleague hasn't stopped talking about it for the past two years, and you're keen to hear what all the hype's about.

Personally speaking, I've experienced all three of the above - and when I was first digging in to TypeScript, I found it quite difficult to find all the answers I needed as a beginner/hobbyist in one place.

I'm hoping that this article can:

* Explain why you should bother learning TypeScript
* Show you the advantages of why I think TypeScript's fantastic
Demonstrate via examples the similarities between JavaScript and TypeScript

## Why bother with TypeScript?

Let's start with an example. Spot the bug in this JavaScript code:

{% gist https://gist.github.com/Sarghm/6cc63c9d47a20c19f58dd19ea751cbd5 %}

Did you spot it? It's a fairly common mistake - our function multiply multiplies a passed number by two…but we've gone and passed it a string.

We shouldn't be multiplying a string - we should be multiplying a number!

What's worse - we could very easily miss this bug. After all, when we run this JavaScript - perhaps embedded as part of a website, or Node.js application, myNumber will just be NaN.

Now, imagine if we passed myNumber to another function, with the intention of it to be used as a number. In the best case scenario, we'll be backtracking to find this not-so-obvious bug, and in the worst, our code is pushed live, and causes unexpected behaviour to our users!

And this, right here, is one of the main reasons TypeScript was created. TypeScript catches these problems before they reach runtime, and instead, are caught at compile time (I'll get into what that means in just a minute…)

## Okay, I'm intrigued. Show me some examples!

Like a lot of developers out there, I learn best through examples. Let's check out some basic code snippets written in JavaScript, then see how each one could be written in TypeScript.

**Example 1: Adding two numbers together**

**JavaScript:**

{% gist https://gist.github.com/Sarghm/51240eca1d2c01455a4bd78bc8a2dc28 %} 

**TypeScript:**

{% gist https://gist.github.com/Sarghm/f3ddeee3b4ee965cd5eef004f548efc3 %}

**Example 2: Getting the first letter of a word**

**JavaScript:**

{% gist https://gist.github.com/Sarghm/ab396e024fcb3b020ad710246f3beb68 %}

**TypeScript:**

{% gist https://gist.github.com/Sarghm/dcddb1c391ffbe7a740c7825092ea965 %}

From these examples, you might be surprised with how similar writing TypeScript is to JavaScript is. At its core, TypeScript is just JavaScript, but with additional features to make life for a developer a little easier.

I'll explain in a bit more detail in just a minute, but first, here's a more complex example:

**Example 3: Create a new object, then access a value on it**

**JavaScript:**

{% gist https://gist.github.com/Sarghm/550f544a132c24ce65bac16a058b7f10 %}

**TypeScript:**

{% gist https://gist.github.com/Sarghm/150b2034a69361d727fc349ca9ae48ad %}

It's what you already know, just with some extra bits and pieces that will make your development life so much easier in the long run.

## Shameless Self-Plug

![Introduction to TypeScript Course Cover Photo](https://i.imgur.com/Zb7PfE3.png)

If you're enjoying reading this - I have a free Introduction to TypeScript video course available over on [my YouTube channel!](https://www.youtube.com/watch?v=vugSEGpPsEo&list=PL7siRj7spTbBfiNQ1kBVuytL8-I8DZWZq)

I'll be uploading new parts to my channel every Wednesday but if you don't want to wait, you can visit [here](https://codesnap.io/introduction-to-typescript) to download and binge the whole thing right away.

Alright, back to the article…

## TypeScript is (not so) secretly just JavaScript

![TypeScript-related programmer humour](https://i.imgur.com/ZLOe4ay.png)

(Sorry, couldn't resist.)

The thing is, we could write TypeScript files to our heart's content; but not a single web browser out there could understand it right now.

And therein lies the prestige. After all is said and done, TypeScript compiles into plain ol' JavaScript.

![Diagram describing the TypeScript compiler](https://i.imgur.com/pw5Delc.png)

TypeScript files are written with the `.ts` file extension, and JavaScript is written with the `.js` file extension. Modern browsers can't understand TypeScript files in their native form. So, we need to turn it into something that it can understand - JavaScript!

This is where the TypeScript compiler comes in.
Once we're ready to ship our TypeScript code, we can run the TypeScript compiler on our `.ts` file(s), with a command like so:

`tsc -w ./index.ts - outFile ./index.js`

After running this on our `index.ts` TypeScript file, we should have an `index.js` file ready that we can import into our website, use as part of a Node.js application, create a Chrome extension with - anything, really, that you can do with JavaScript!

What's more, the compiler has another feature. It'll also tell us when we write bugs in our code, and even let us know how to fix it…

## Uncovering bugs early

Let's take that JavaScript example from the top of this article:

{% gist https://gist.github.com/Sarghm/6cc63c9d47a20c19f58dd19ea751cbd5 %}

…and let's write out what it would look like in TypeScript…

{% gist https://gist.github.com/Sarghm/c307b3e891fe9600fb3e8f99bf6ed342 %}

Now, let's see what happens when we try to compile our TypeScript code above with the TypeScript compiler, as we learned in the previous section…

`tsc ./index.ts - outFile ./index.js`

![Terminal readout demonstrating a bad TypeScript compile](https://i.imgur.com/cMTx5q3.png)

Uh-oh. This can't be good.

TypeScript created our `index.js` file, but it tells us there's something wrong with our code. It's explicitly telling us that we can't pass a string to a function which requires our value argument to be a number…so let's fix that bug!

{% gist https://gist.github.com/Sarghm/c03a8fd1a0a8ed43cb86272c5d4da1ac %}

Now, when we run it once again…

![TypeScript compiler showing no errors](https://i.imgur.com/Y9LojyZ.png)

Hey, look! No errors!

We can now deploy our `index.js` file to wherever we want with the confidence that we don't have any type errors in our codebase.

## Summary

This is the tip of the iceberg. Not even the tip - it's almost like an ice-cube balanced on top of an iceberg. If you're excited by this, then I promise you that there's much more to be excited by with TypeScript. It's insanely convenient and helpful, but also ridiculously powerful.

What's more; almost all of the major frontend frameworks (React.js, Vue, Angular) have TypeScript support nowadays - same with backend frameworks, too. You'll be able to write your TypeScript using the latest JavaScript features, but deploy JavaScript that works almost anywhere!

That's all for this introduction - but I'll be posting more articles about TypeScript in the coming weeks.

![CodeSnap Logo](https://i.imgur.com/QO9lI9f.png)

In the meantime, if you're interested in learning more TypeScript - my Introduction to TypeScript video course is available on my [website, CodeSnap.io](https://codesnap.io/introduction-to-typescript). 

It's got basically everything I've mentioned here stuffed into the first two videos - and there's sixteen videos in the entire course.

If you're interested in checking out the course on YouTube, the playlist is available here, or, if you'd rather download and binge the entire thing, you can [download it from here](https://codesnap.io/introduction-to-typescript).

Thanks so much for reading - happy TypeScript'ing!