GyanMilega

πŸ“˜ TypeScript: Day 2 – Interfaces & Object Types

Author: Sagar Kudu
Sagar Kudu

 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:

  1. Create an interface Person with:

    • name (string),

    • age (number),

    • isStudent (boolean, optional).
      Then make two objects with it.

  2. Create an interface Movie with:

    • title (string),

    • year (number),

    • readonly id (number).
      Make one movie object, try changing the id (see what happens).

  3. Create an interface MathOps with a method multiply(a: number, b: number): number.
    Implement it and call it.

Solution:

  1. 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 }
    
  1. Create an interface Movie with:

  • title (string),

  • year (number),

  • readonly id (number).
    Make one movie object, try changing the id (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 and year are normal, so you can update them.

    πŸ–₯ Output:

    { id: 1, title: 'Bahubali', year: 2020 }
    { id: 1, title: 'Chota Chetan', year: 2020 }
    
  1. Create an interface MathOps with a method multiply(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 a multiply 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

Powered by wisp

Related Posts

πŸ“˜TypeScript: Day 1 – Variables, Types & Functions

TypeScript: Variables, Types & Functions

Read Full Story

What 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 Story

What 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
Loading...
Β© Gyan Milega 2025