Basic types

Learn the basics of TypeScript and how to define simple types

An Intro To TypeScript #

Now that you have a clear idea of what JavaScript looks like in 2019, I think it’s time to introduce you TypeScript.

Albeit not necessary as a skill for a JavaScript developer, I believe it to be extremely useful, especially when working in team on bigger projects.

As you already know, JavaScript is not a strongly typed language meaning that you don’t have to define the type of your variables upon declaration.

This means that they can be more flexible and accept different values. At the same time it can make the code more confusing and prone to having bugs.

Look at this example:

Press + to interact
function getUserByID(userID){
// perform an api call to your server to retrieve a user by its id
}

What is userID ? Is it an integer or a string? We can assume it’s an integer, but maybe it’s an alphanumeric string (e.g.: ‘A123’).

Unless we wrote that piece of code, we have no way of knowing what the type of the argument is.

That’s where TypeScript comes in handy. This is how the same code would look:

Press + to interact
function getUserByID(userID:number){
// perform an api call to your server to retrieve a user by its id
}

Perfect! Now we know for sure that if we pass a string to the function, that will cause an error.

A simple addition made the code easier to use.

 

What is TypeScript? #

Created just a few years ago by Microsoft, TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

Being a superset ...

Access this course and 1400+ top-rated courses and projects.