In this section, we're going to learn about TypeScript.
TypeScript is a programming language primarily used with angular.
It's completely optional to use TypeScript.
However, I highly recommend learning it.
A majority of projects, tutorials and examples are written with TypeScript.
It's hard to escape.
So if you want to learn angular learning, TypeScript is a must.
If you already know TypeScript.
Feel free to skip this section of the course.
I'm not going to be teaching anything groundbreaking as long as you know the basics.
You should be good to go.
Otherwise, let's learn TypeScript together.
So what is TypeScript?
TypeScript is a superset of JavaScript.
It is a language built on top of each JavaScript language.
If you know JavaScript, you know, TypeScript features such as variables, conditional statements and
functions are written the same way in TypeScript.
We don't need to learn an entirely new set of syntax rules for getting started.
In fact, we can rename our files to TypeScript without modifying the original source code.
Our JavaScript code would still work.
Instead, TypeScript adds features to improve the developer experience.
Notice how I said developer experience TypeScript doesn't add features for enhancing the performance
or security of an app.
Instead, it's a language designed to help developers debug and design their applications.
It accomplishes this goal by introducing static typing to the language as we know data types or categories
for our data.
We have strings, numbers, Boolean and objects.
These are just some examples of data types.
For the most part, we never have to worry about the data type of our variables.
JavaScript is a dynamically typed language.
We have the opportunity to change the data type of our variables at any point in our application.
Here's an example.
We have a variable called price.
The data type starts as a number online, too.
The variable is updated to a string behind the scenes.
The environment will change the data type on our behalf.
It won't complain if we attempt to do something like this.
This feature is convenient.
It's one of the reasons why JavaScript is beginner friendly.
While suitable, there are some pitfalls beginners can fall into in a large application.
Data is constantly flowing through different files, objects, and functions.
Let's say we're developing a checkout system.
The user will want the grand total for their purchase.
However, before we present them with a total, we may want to run their purchase price through several
functions.
For example, what if they're using a coupon?
We should apply the coupon to the price first.
Not so fast.
We need to calculate taxes.
Don't forget to add the cost of shipping during this entire process.
The price will be passed around through a couple of functions.
It's possible that the data type may change during this process.
If it does, we may get unexpected behavior.
Our functions assume the price will be a number.
What if it changes to a string?
JavaScript won't throw an error.
It'll allow the data type to change even when we don't want it to.
We'll only find out if the data type changes by testing the app in the browser.
Wouldn't it be convenient to catch this type of error in our code while writing it?
That's possible with TypeScript.
We'll get into TypeScript in a moment.
But let's see how this error can happen in our script.
I prepared a little example.
It's a single file called example, not jazz.
In this example, I have a function called add shipping.
It has two arguments called price and shipping.
The price argument refers to the price of the product.
The shipping argument refers to the cost of shipping inside the function, where logging the two arguments
after they've been added together, the function should log a number below the function definition.
We're calling this function with the numbers 10 and five.
Let's try running this script and the command line.
I'll run the script with the following command.
As expected, we got 15 perfect, right?
Why don't we pass in a string instead of a no back in our file all wrapped the first number with some
quotes?
Next hour, rerun the script.
This time we got one hundred and five.
If I were a customer, I'd be turned off by this calculation.
This type of error occurred because of how we've written our code.
We passed in a string when we meant to pass in a number.
TypeScript is designed to catch errors like these.
It's a language that can check the data types of our variables.
It's much stricter than JavaScript, but worth it.
In the end, we can improve the developer experience by switching to TypeScript.
Let's dive into TypeScript in the next lecture.