A type annotation follows a variable name with a colon. Common primitive
types are string, number and boolean.
Annotations
TypeScript
let score: number = 0; let playerName: string = "Ada"; let isComplete: boolean = false;
Inference
You do not need an annotation when TypeScript can clearly infer one from the
initial value. In the following code, score is inferred as a
number. TypeScript will reject assigning a string later.
TypeScript
let score = 0; score += 10;
Avoiding any
The any type turns off checking for a value. It can be useful
temporarily at an untyped boundary, but it spreads uncertainty through a
program. Prefer unknown when a value is genuinely not known:
it forces you to check the value before using it.
Did you know my courses are all printer-friendly? This means you can easily export them to PDF or print them for later use.
