1. Welcome and Icebreaker:
- If you could extend any object in your life with new features, what would you add?
2. Outline the Session:
- We’ll learn how to extend interfaces in TypeScript to build on existing contracts.
- Learning Outcomes: Understand how to use
extends to create more specific interfaces from general ones.
- Let’s build on our knowledge!
3. Concept Explanation:
- Extending Interfaces allows you to create new interfaces that inherit properties and methods from one or more existing interfaces.
- Use the
extends keyword.
- This helps you build more specific types from general ones, promoting code reuse and flexibility.
4. Live Coding Demonstration:
interface Person {
name: string;
}
interface Employee extends Person {
employeeId: number;
}
const emp: Employee = {
name: "Alice",
employeeId: 123
};
console.log(emp);
// Extending multiple interfaces
interface A { a: number; }
interface B { b: string; }
interface C extends A, B {
c: boolean;
}
const obj: C = { a: 1, b: "test", c: true };
console.log(obj);
5. Tasks:
- Short Exercise:
- Define an interface
Shape with a property color (string).
- Define an interface
Circle that extends Shape and adds a property radius (number).
- Create a variable of type
Circle and print its properties.