0
0
Typescriptprogramming~5 mins

Merging classes with interfaces in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to merge a class with an interface in TypeScript?
It means adding extra type information or members to a class by declaring an interface with the same name. The class and interface combine, so the class must satisfy both its own members and the interface's members.
Click to reveal answer
beginner
How do you declare an interface to merge with an existing class named <code>Person</code>?
You declare an interface with the exact same name: <br><pre>interface Person { <br>  newProperty: string; <br>}</pre> This adds <code>newProperty</code> to the <code>Person</code> class instance type.
Click to reveal answer
intermediate
Can merged interfaces add properties to the class's instance type or static type?
Merged interfaces add properties to the class's instance type, not the static type. To add static members, you must merge with the class's constructor type separately.
Click to reveal answer
intermediate
What happens if the class does not implement the members declared in the merged interface?
TypeScript will show a type error because the class instance does not satisfy the interface's contract. You must implement all interface members in the class or provide them via other means.
Click to reveal answer
beginner
Why is merging classes with interfaces useful in TypeScript?
It allows you to extend or add type information to existing classes without modifying their original code. This is helpful for adding properties or methods for typing purposes, especially with third-party libraries.
Click to reveal answer
What must an interface have to merge correctly with a class in TypeScript?
AThe same name as the class
BThe same properties as the class
CA different name than the class
DOnly static members
When merging an interface with a class, what type of members does the interface add?
APrivate members
BStatic members
CConstructor parameters
DInstance members
If a class Car is merged with an interface adding a property color, what must the class do?
AIgnore the <code>color</code> property
BImplement the <code>color</code> property
CRename the class
DAdd a static <code>color</code> property
Can merging interfaces add static members to a class type directly?
AYes, always
BOnly if the interface uses the static keyword
CNo, static members require merging with the constructor type
DOnly if the class is abstract
Why might you merge an interface with a class from a third-party library?
ATo add extra type information without changing the original code
BTo rename the class
CTo remove properties from the class
DTo convert the class to a function
Explain how merging a class with an interface works in TypeScript and why it is useful.
Think about how interfaces and classes combine by name and what that means for the class type.
You got /4 concepts.
    Describe the difference between adding instance members and static members when merging interfaces with classes.
    Consider what part of the class the interface affects.
    You got /3 concepts.