Type Annotation
You explicitly write the type.
Example: let age: number = 25
These are the basic building blocks:
let age: number = 25;let fullName: string = "Sahil";let isActive: boolean = true;let empty: null = null;let notSet: undefined = undefined;TypeScript checks if values match your declared type.
let value: any = 10;value = "text";value.toUpperCase(); // No error, even if runtime may failany disables type safety.
let value: unknown = "text";
if (typeof value === "string") { value.toUpperCase();}unknown forces you to check type before use.
Type Annotation
You explicitly write the type.
Example: let age: number = 25
Type Inference
TypeScript guesses from value.
Example: let age = 25
With strict mode, null and undefined are not automatically allowed.
let username: string | null = null;username = "sahil";Without | null, assigning null will fail.
Literal types restrict a value to exact options.
let direction: "left" | "right";direction = "left"; // valid// direction = "up"; // errorGreat for status values, UI modes, and action names.
let id: string | number;This means id can be one of the allowed types.