Let's get started with Typescript.
We will use Typescript online playground for this demos:
http://www.typescriptlang.org/play/
- Let's start with something very basic, if we paste this code on codepen (javascript):
let a = 3;
let b = 5;
b= "5";
const result = a + b;
console.log(result);
Instead of getting 8 as expected result, we will get the following result: 35
- Let's paste this code into typescript playground and we will notice that we get type checking for free.
Type already infers that a and b vars are typed (number) and throws an error indicating that the b assignment "5" is wrong.
- We can be more explicit and define the types:
- let a = 3;
+ let a : number = 3;
- let b = 5;
+ let b : number = 5;
b= 5;
const result = a + b;
console.log(result);