0
0
Typescriptprogramming~10 mins

Merging classes with interfaces in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Merging classes with interfaces
Define class
Define interface with same name
TypeScript merges interface into class type
Class instances have combined type
Use merged properties and methods
TypeScript merges an interface with the same name as a class, combining their types so class instances have all properties and methods.
Execution Sample
Typescript
class User {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}
interface User {
  age: number;
}
Defines a class User and an interface User with an extra property; TypeScript merges them so User instances have both.
Execution Table
StepActionEvaluationResult
1Define class User with property name and constructorClass User createdUser has property 'name' and constructor
2Define interface User with property ageInterface User createdInterface User has property 'age'
3TypeScript merges interface User into class User typeMerged type includes 'name' and 'age'User type now has properties 'name' and 'age'
4Create instance: const u = new User('Alice')Instance u createdu.name = 'Alice', u.age is undefined (needs assignment)
5Assign u.age = 30Property age assignedu.age = 30
6Access u.name and u.ageRead propertiesOutputs: name='Alice', age=30
7End of exampleNo more stepsExecution stops
💡 All steps executed; merged class and interface types combined successfully
Variable Tracker
VariableStartAfter Step 4After Step 5Final
u.nameundefined'Alice''Alice''Alice'
u.ageundefinedundefined3030
Key Moments - 2 Insights
Why does the instance 'u' not have 'age' immediately after creation?
Because the class constructor only sets 'name'. The 'age' property comes from the merged interface and must be assigned separately, as shown in step 5.
How does TypeScript know to merge the interface with the class?
TypeScript merges declarations with the same name automatically, combining their types into one, as seen in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'u.age' after step 4?
Aundefined
B30
C0
D'Alice'
💡 Hint
Check the variable_tracker column 'After Step 4' for 'u.age'
At which step does 'u.age' get assigned a value?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the execution_table row describing property assignment
If the interface 'User' had a method instead of a property, what would happen?
AThe method would be ignored
BThe class would automatically have the method implementation
CTypeScript would merge the method signature, but class must implement it
DThe code would cause a runtime error
💡 Hint
Recall that interfaces add type info but do not provide implementations
Concept Snapshot
Merging classes with interfaces in TypeScript:
- Define a class and an interface with the same name.
- TypeScript merges their types automatically.
- Class instances have combined properties.
- Interface adds type info; class provides implementation.
- Properties from interface must be assigned if not in constructor.
Full Transcript
This example shows how TypeScript merges a class and an interface with the same name. First, a class User is defined with a name property and constructor. Then, an interface User is defined with an age property. TypeScript merges these so the User type has both name and age. When creating an instance u of User, only name is set by the constructor. The age property must be assigned separately. This merging allows extending class types with interfaces for flexible typing.