Adding types ( : )
Specifies a type for the variable.
Syntax
let name: type = value;
Description
You declare the variable's type with a colon. If you assign the wrong type, TypeScript throws an ERROR (without running the code). Basic types: number, string, boolean.
Example
Loading editor…
Expected output
Alex 20
When to use?
Catching errors early, documenting code.
Common mistakes
- ⚠️ let age: number = "20"; → ERROR: string cannot be assigned to number.