0
0
Typescriptprogramming~10 mins

Extending interfaces in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extending interfaces
Define Base Interface
Define Extended Interface
Extended Interface has all Base + new members
Use Extended Interface in code
Access all properties from Base and Extended
You start with a base interface, then create a new interface that adds more properties by extending the base. The extended interface includes all properties from the base plus new ones.
Execution Sample
Typescript
interface Animal {
  name: string;
}
interface Dog extends Animal {
  breed: string;
}
const myDog: Dog = { name: "Buddy", breed: "Beagle" };
This code defines a base interface Animal and extends it with Dog, adding a breed property. Then it creates an object myDog with both properties.
Execution Table
StepActionEvaluationResult
1Define interface Animal with property name:stringAnimal = { name: string }Animal interface ready
2Define interface Dog extending Animal adding breed:stringDog = Animal + { breed: string }Dog interface ready with name and breed
3Create object myDog of type Dog with name and breedmyDog = { name: "Buddy", breed: "Beagle" }myDog object created successfully
4Access myDog.namemyDog.name"Buddy"
5Access myDog.breedmyDog.breed"Beagle"
6End of code executionAll properties accessed correctly
💡 All interface properties are defined and accessible; no errors.
Variable Tracker
VariableStartAfter Step 3Final
Animalundefined{ name: string }{ name: string }
Dogundefinedextends Animal + { breed: string }extends Animal + { breed: string }
myDogundefined{ name: "Buddy", breed: "Beagle" }{ name: "Buddy", breed: "Beagle" }
Key Moments - 2 Insights
Why can myDog have both name and breed properties?
Because Dog extends Animal, it includes all properties from Animal (name) plus its own (breed). See execution_table rows 2 and 3.
What happens if myDog misses the breed property?
TypeScript will give an error because Dog requires both name and breed. This is shown in execution_table row 3 where both are provided.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of myDog.name at step 4?
A"Buddy"
B"Beagle"
Cundefined
DError
💡 Hint
Check execution_table row 4 where myDog.name is accessed.
At which step is the Dog interface defined to include properties from Animal?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 2 where Dog extends Animal.
If you remove the breed property from myDog, what will happen?
ANo error, code runs fine
BmyDog will have breed as undefined
CTypeScript error because breed is missing
DOnly name property is required
💡 Hint
Refer to key_moments second question and execution_table row 3.
Concept Snapshot
interface Base { ... }
interface Extended extends Base { ... }
Extended has all Base properties plus new ones.
Use Extended to create objects with full set.
Missing required properties causes errors.
Full Transcript
This example shows how to extend interfaces in TypeScript. First, we define a base interface Animal with a name property. Then, we create a Dog interface that extends Animal and adds a breed property. When we create an object myDog of type Dog, it must have both name and breed. Accessing these properties works as expected. Extending interfaces lets you build on existing types by adding new properties, ensuring type safety and code reuse.