Skip to content

Objects, Interfaces, and Type Aliases

let user: { name: string; age: number } = {
name: "Sahil",
age: 22,
};

This works, but is hard to reuse.


interface User {
name: string;
age: number;
}

Use interfaces to describe object shapes clearly.


type User = {
name: string;
age: number;
};

Type aliases can also represent unions, tuples, and complex type formulas.


  • Great for object-oriented style design
  • Supports declaration merging (reopening)
  • Common for class contracts

interface User {
name: string;
}
interface User {
age: number;
}

Now User has both name and age.


type A = { name: string };
type B = { age: number };
type Person = A & B;

Person must contain both sets of properties.

graph LR A[Type A] --> C[Intersection Type] B[Type B] --> C C --> D[Combined Requirements]

interface Calculator {
add(a: number, b: number): number;
}

Method signatures keep behavior contracts explicit.