TypeScript is an open-source language that builds on JavaScript, one of the world’s most used tools, by adding static type definitions. And typescript is a superset of javascript.
There are a ton of programming languages. Although JavaScript is the dominant language on the web.
In JavaScript,
var a = 100;
You see a dynamically typed language
allows us to not have to say what type of variable.
In C++
int a;
a = 100;
A statically typed language like c++ we have to declare type of a variable.
Dynamically typed languages, type
checking is done during runtime.
We are able to use dynamic typed languages that can assign anything to any variable and you are not going to be caught. You might get errors during runtime while users browsing the web.
- some people think statically typed languages are the way to go.
- some people think statically typed languages are a waste of time.
TypeScript Pros and Cons
Pros
- Statically typed languages are self documenting.
function sum(a: number, b: number) {
return a + b;
}
sum("hello", 5)
- string is not assignable to parameter of type ‘number’.
2. Many IDE help with auto completion.
3. The main thing of statically typed languages is that you are going to get less bugs in production.
Cons
- You just made our code a little bit harder to read and complex.
- You just need to write better tests.
- Many people get excited about static typing and skip writing a test.
- You are going to have a slower development process because you now have extra steps.
- The main thing static types usually prevent bugs and help keep errors from happening dynamic typing allows you to be more flexible and write software faster.
- TypeScript allows us to make JavaScript behave like a statically typed language it adds types to JavaScript.
Install TypeScript via npm
$ npm install -g typescript
$ tsc -version
Version 4.0.3
Execute TypeScript
- Convert TS file to JS file and execute
$ tsc main.ts && node main.js
2. Use ts-node
$ npm install -g ts-node
$ ts-node main.ts