Is basically telling what type something is.
For example let say we have a box, and we want to tell people to only put eggs in the box.
let myBox; // π¦
In real life we would put a sticker in the box saying βonly eggs please πβ, in TypeScript we do the same, we add a βtypeβ saying what something is and only accepts.
Variable Type value
ββββββ΄ββββββ ββββββ΄ββββββ βββββ΄ββββ
let myBox_π¦ : onlyEggs_π = "π₯π₯π₯";
β β
Separator Assignment operator
There are 3 basic types in TypeScript
let isDone: boolean = false; // π or π
let lines: number = 42; // 0οΈβ£1οΈβ£2οΈβ£3οΈβ£4οΈβ£5οΈβ£...
let name: string = "John"; // π
When itβs impossible to know, there is the βAnyβ type
let notSure: any = 4; // π€·ββοΈ Not sure
notSure = "I'm a string"; // I can change it later
notSure = false; // maybe a boolean
There are typed arrays
let list: number[] = [1, 2, 3];
// or with emojis:
Only chickens please!
βββ΄ββ
let chickens: π[] = [π£,π€,π₯,π];
Alternatively you can use Array
let list: Array<number> = [1, 2, 3];
// or with emojis
let listOfChickens: Array<π> = [π£,π€,π₯,π];
Enumerations also known as enums:
enum Square { Red, Green, Blue }; // π₯, π©, π¦
// This can be understood better by seeing what Enumerations
// are compiled to in JavaScript:
Square = {
0 : 'Red',
1 : 'Green',
2 : 'Blue',
Red : 0,
Green : 1,
Blue : 2,
};
// Now that you know is just an object,
// you can access it by name or number.
console.log( Square.Green ); // π©
console.log( Square[2] ); // π₯
// or in a more complex way
let c: Square = Square.Blue;
console.log( Square[c] ); // π¦