Interfaces, type aliases & objects
Describing object shapes, optional and readonly properties, and interface vs type.
An interface or a type alias describes the shape of an object — which properties it has and their types.
?marks an optional property:remote?: boolean.readonlystops a property being changed after creation.- Interfaces can
extendsother interfaces; type aliases combine with&(intersection).
interface vs type: both describe objects. Interfaces can be extended and merged; type aliases can also describe unions, tuples and primitives. Many teams use interfaces for objects and types for everything else — the important thing is to be consistent.
Example code
Try it in the TypeScript Playground or with tscinterface Job {
readonly id: number;
title: string;
city: string;
salaryLpa?: number; // optional
}
interface RemoteJob extends Job {
timezone: string;
}
const job: Job = { id: 1, title: "Frontend Developer", city: "Kochi" };
job.title = "Senior Frontend Developer"; // OK
job.id = 2; // error
const remote: RemoteJob = { id: 3, title: "QA", city: "Remote", timezone: "IST" };
console.log(job.title, "|", remote.timezone);
Compiler / program output
error TS2540: Cannot assign to 'id' because it is a read-only property.
(after removing that line)
Senior Frontend Developer | IST
Paste the code into the official TypeScript Playground in your browser, or install TypeScript (npm install -D typescript) and run npx tsc --noEmit to type-check. Names are fictional.
Key points
- Interfaces and type aliases describe object shapes.
- ? = optional, readonly = can't be reassigned.
- Interfaces extend; types combine with &.
- Pick one convention and use it consistently.
Quick check
What does salaryLpa?: number mean?
The ? makes the property optional, so its type is effectively number | undefined.
Practice task
Create an interface Candidate with name, email, skills (string array) and an optional portfolioUrl, then write a function that returns a one-line summary of a candidate.
Interview corner
What is the difference between an interface and a type alias?
Both describe object shapes. Interfaces support extends and declaration merging; type aliases can also name unions, tuples and primitives and combine with intersections. For plain objects they're largely interchangeable.
What does readonly do?
It prevents reassigning a property after the object is created, at compile time. It doesn't freeze the object at runtime.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.