Type annotations are explicit types you write after a colon, as in let age: number. They tell TypeScript what type a variable, parameter or return value must have, and the compiler rejects any value that doesn't fit.
// Variable annotations
let username: string = "john";
let age: number = 25;
let isLoggedIn: boolean = false;
// Function parameter and return type annotations
function greet(name: string): string {
return `Hello, ${name}!`;
}
// Array annotations
let numbers: number[] = [1, 2, 3];
let names: Array<string> = ["Alice", "Bob"];
Array types take either form, and string[] means the same as Array<string>.
In practice you annotate function parameters and often return types. You let type inference handle most local variables, because an annotation that repeats an obvious initial value adds nothing.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓