1. Type Annotation
TypeScript uses colon ( : ) behind variables to annotate a type
const myMaxHamburger: number = 3;
const answer: string = "Yes I can";
const drinks: boolean = true;
You can code like variable: <type>
It is essential to write this annotation, so be careful to pass by this
1_01. Type Declaration_string
let str: string;
let red: string = 'Red';
let green: string = 'Green';
let myColor: string = `Do you like Color ${green}?`;
You can code like let <variable name>: <type>
String type is just a letter that can't be calculated
1_02. Type Declaration_number
let num: number;
let integer: number = 6;
let float: number = 3.14;
let hex: number = NaN;
In Java, you can't use integers as float. But in Typescript, we can use them equally
1_03. Type Declaration_union type
let name: string | number = 'john';
let name: string | number = 123;
let name: string[] | number = ['test1', 'test2', 'test3'];
You can grammarly understand this as "You can use either A or B"
For example, we can call the name either string or number.
Therefore, it can be more flexible
1_04. Type Declaration_boolean
let isBool: boolean;
let isDone: boolean = false;
const size: number = 101;
const isHuge: boolean = size >= 99; // <- this area return as boolean type
End
Next Series