0
0
Typescriptprogramming~5 mins

Type compatibility with classes in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does type compatibility mean in TypeScript classes?
It means one class type can be used where another is expected if their structures match, even if they are different classes.
Click to reveal answer
beginner
How does TypeScript check compatibility between two classes?
TypeScript compares the properties and methods of the classes. If one class has at least the same members with compatible types, it is compatible.
Click to reveal answer
intermediate
Can private or protected members affect class compatibility in TypeScript?
Yes. If classes have private or protected members, they must come from the same declaration to be compatible.
Click to reveal answer
beginner
Given two classes with the same public properties, are they compatible?
Yes. If their public properties and methods match in type, they are compatible regardless of class names.
Click to reveal answer
intermediate
Why does TypeScript use structural typing for classes instead of nominal typing?
Because it focuses on the shape of data (members) rather than the class name, making code more flexible and reusable.
Click to reveal answer
In TypeScript, when are two classes considered compatible?
AWhen their public members have matching types
BOnly if they have the same class name
CIf they are declared in the same file
DWhen they have the same number of methods
What happens if two classes have private members with different declarations?
APrivate members are ignored in compatibility
BThey are always compatible
CThey are incompatible
DThey become compatible if public members match
Which typing system does TypeScript use for class compatibility?
ANominal typing
BStructural typing
CDuck typing
DDynamic typing
If class A has extra public members not in class B, can A be assigned to B?
AYes, if B's members are present in A
BNo, extra members prevent compatibility
COnly if classes share the same name
DOnly if extra members are private
Why might TypeScript's class compatibility be helpful in real projects?
AIt forces strict class inheritance
BIt disables private members
CIt requires all classes to have unique names
DIt allows flexible use of objects with similar shapes
Explain how TypeScript determines if two classes are compatible.
Think about comparing the shape of the classes, not their names.
You got /4 concepts.
    Describe a situation where two classes with the same public properties are not compatible.
    Consider private members and their declarations.
    You got /3 concepts.