π TypeScript: Day 2 β Interfaces & Object Types

Did you remember? We used inline object typing like this:
const car: { brand: string; year: number } = {
brand: "Tata",
year: 2025
};
That works fine, but imagine you have lots of objects with the same shape. Typing { brand: string; year: number }
again and again is messy.
π This is where interfaces come in.
1. Interfaces (the clean way)
interface Car {
brand: string;
year: number;
}
const car1: Car = { brand: "Tata", year: 2025 };
const car2: Car = { brand: "Tesla", year: 2024 };
console.log(car1) // { brand: 'Tata', year: 2025 }
console.log(car2) // { brand: 'Tesla', year: 2024 }
2. Optional Properties
Sometimes an object might not have all properties:
interface User {
name: string;
age?: number; // optional
}
const user1: User = { name: "Sagar" };
const user2: User = { name: "Sakshi", age: 30 };
console.log(user1) // { name: 'Sagar' }
console.log(user2) // { name: 'Sakshi', age: 30 }
3. Readonly Properties
If you donβt want something to change:
interface Book {
readonly id: number;
title: string;
}
const book: Book = { id: 1, title: "TypeScript Basics" };
console.log(book) // { id: 1, title: 'TypeScript Basics' }
book.title = "Advanced TS"; // β
OK
// book.id = 2; β Error: Cannot assign to 'id' because it is a read-only property
4. Functions Inside Interfaces
Interfaces can also describe methods:
interface Calculator {
add(a: number, b: number): number;
}
const calc: Calculator = {
add(a, b) {
return a + b;
}
};
console.log(calc.add(5, 3)) // 8
π Exercises
Try these in the Playground or your setup:
Create an interface
Person
with:name
(string),age
(number),isStudent
(boolean, optional).
Then make two objects with it.
Create an interface
Movie
with:title
(string),year
(number),readonly id
(number).
Make one movie object, try changing theid
(see what happens).
Create an interface
MathOps
with a methodmultiply(a: number, b: number): number
.
Implement it and call it.
Solution:
Create an interface
Person
with:name
(string),age
(number),isStudent
(boolean, optional).
Then make two objects with it.
Solution: π¨βπ©βπ§ Person Interface
Blueprint:
interface Person { name: string; age: number; isStudent?: boolean; // optional }
Objects:
const person1: Person = { name: "Sagar", age: 20, isStudent: true }; const person2: Person = { name: "Sakshi", age: 15 };
π§Έ Explanation:
isStudent
is optional β person2 can skip it.TypeScript checks that both objects follow the Person blueprint.
π₯ Output:
{ name: 'Sagar', age: 20, isStudent: true } { name: 'Sakshi', age: 15 }
Create an interface
Movie
with:
title
(string),year
(number),readonly id
(number).
Make one movie object, try changing theid
(see what happens).
π¬ Movie Interface
Blueprint:
interface Movie { readonly id: number; title: string; year: number; }
Object:
const movie1: Movie = { id: 1, title: "Bahubali", year: 2020 };
Updating fields:
movie1.title = "Chota Chetan"; // β OK // movie1.id = 2; β Error, id is readonly
π§Έ Explanation:
id
is like a movieβs unique ticket number β canβt be changed.title
andyear
are normal, so you can update them.
π₯ Output:
{ id: 1, title: 'Bahubali', year: 2020 } { id: 1, title: 'Chota Chetan', year: 2020 }
Create an interface
MathOps
with a methodmultiply(a: number, b: number): number
.
Implement it and call it.
π’ MathOps Interface
Blueprint:
interface MathOps {
multiply(a: number, b: number): number;
}
Object:
const result: MathOps = {
multiply(a, b) {
return a * b;
}
};
console.log(result.multiply(2, 3));
π§Έ Explanation:
You told TypeScript: βAny object of type
MathOps
must have amultiply
method.βThe object
result
implements that method.Works like a mini calculator.
π₯ Output:
6
β
All 3 exercises are 100% completed.
You used:
Optional (
?
) βRead-only (
readonly
) βFunction in interface β
Also Learn
πTypeScript: Day 1 β Variables, Types & Functions
TypeScript: Variables, Types & Functions
Read Full StoryWhat is TypeScript?
TypeScript is a superset of JavaScript that adds static typing to the language. This means you can define the type of a variable, function parameter, or object property, allowing the compiler to catch type-related errors before your code runs. This leads to more robust and maintainable code.
Read Full Storyπ Day 3 β TypeScript: Union Types, Type Aliases & Enums
Unlock the power of **Union Types**, **Type Aliases**, and **Enums** in TypeScript to write cleaner, more flexible code. With **Union Types**, you can let variables hold multiple types, making your code more versatile. **Type Aliases** make complex types easier to read and reuse, while **Enums** help you manage related constants in a more organized way. These features give you the tools to build robust and maintainable applications.
Read Full StoryWhat is a Function in JavaScript? | Function Examples & Guide
Functions in a JavaScript. Learn what functions are in JavaScript with examples. This beginner-friendly guide covers function declarations, parameters, return values, and more.
Read Full Story