Skip to content

Type System Fundamentals

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 fail

any disables type safety.


Type Annotation

You explicitly write the type. Example: let age: number = 25

Type Inference

TypeScript guesses from value. Example: let age = 25

graph LR A[Variable Declaration] --> B{Type Written?} B -->|Yes| C[Use Annotation] B -->|No| D[Infer From Value]
  • Let TypeScript infer local variables.
  • Add explicit types on function parameters and public APIs.

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"; // error

Great for status values, UI modes, and action names.


let id: string | number;

This means id can be one of the allowed types.