Bird
Raised Fist0
Typescriptprogramming~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. In TypeScript, when are two classes considered compatible for assignment?
      easy
      A. When they have the same number of methods
      B. Only if they share the same class name
      C. When their instances have matching properties and types
      D. If they are declared in the same file

      Solution

      1. Step 1: Understand Type Compatibility

        TypeScript checks if the properties and their types in one class instance match those expected by another class type.
      2. Step 2: Check Class Names and Methods

        Class names or method counts do not affect compatibility; only the shape (properties and types) matters.
      3. Final Answer:

        When their instances have matching properties and types -> Option C
      4. Quick Check:

        Compatibility = matching properties and types [OK]
      Hint: Focus on properties and types, not class names [OK]
      Common Mistakes:
      • Assuming class names must match
      • Thinking method count matters
      • Believing file location affects compatibility
      2. Which of the following is the correct syntax to assign an instance of class B to a variable typed as class A in TypeScript, assuming compatibility?
      easy
      A. let a: B = A();
      B. let a: A = new B();
      C. let a = A(new B());
      D. let a = new A(B);

      Solution

      1. Step 1: Review TypeScript Assignment Syntax

        To assign an instance of class B to a variable typed as A, use let a: A = new B(); if B is compatible with A.
      2. Step 2: Identify Incorrect Syntax

        Other options misuse constructor calls or type annotations and are invalid.
      3. Final Answer:

        let a: A = new B(); -> Option B
      4. Quick Check:

        Use let var: Type = new Class(); [OK]
      Hint: Use variable: Type = new Class() for assignments [OK]
      Common Mistakes:
      • Calling classes like functions
      • Swapping variable and type names
      • Using incorrect constructor syntax
      3. Consider the following TypeScript code:
      class A { x: number; }
      class B { x: number; y: string; }
      
      let a: A = new B();
      console.log(a.x);
      What will be the output when this code runs?
      medium
      A. undefined
      B. Runtime error
      C. The value of x from B instance
      D. Compilation error

      Solution

      1. Step 1: Analyze Class Properties and Assignment

        Class B has properties x and y. Class A has only x. Assigning new B() to a variable typed as A is allowed because B has at least the properties of A.
      2. Step 2: Check Output of console.log(a.x)

        The property x exists on the B instance. Since no value is assigned to x, it is undefined, so console.log(a.x) outputs "undefined".
      3. Final Answer:

        undefined -> Option A
      4. Quick Check:

        Compatible class instance property accessed correctly [OK]
      Hint: Compatible class instances share accessible properties [OK]
      Common Mistakes:
      • Expecting runtime error due to extra properties
      • Thinking assignment causes compilation error
      • Assuming class properties are auto-initialized to 0
      4. Given the code below, why does TypeScript show an error?
      class A { x: number; }
      class B { y: string; }
      
      let a: A = new B();
      medium
      A. Because new B() is not a valid constructor call
      B. Because class names differ
      C. Because B has extra property y
      D. Because B does not have property x required by A

      Solution

      1. Step 1: Check Property Compatibility

        Class A requires property x. Class B only has property y, so B instances lack x.
      2. Step 2: Understand TypeScript Compatibility Rules

        TypeScript requires the assigned instance to have all properties of the target type. Missing x causes error.
      3. Final Answer:

        Because B does not have property x required by A -> Option D
      4. Quick Check:

        Missing required property causes error [OK]
      Hint: Check if assigned class has all required properties [OK]
      Common Mistakes:
      • Thinking class names must match
      • Assuming extra properties cause error
      • Believing constructor call is invalid
      5. You have two classes:
      class Person { name: string; age: number; }
      class Employee { name: string; age: number; salary: number; }
      Which assignment is valid and why?
      let p: Person = new Employee();
      let e: Employee = new Person();
      hard
      A. Only let p: Person = new Employee(); is valid because Employee has all Person properties
      B. Only let e: Employee = new Person(); is valid because Person has fewer properties
      C. Both assignments are valid because classes share some properties
      D. Neither assignment is valid due to different class names

      Solution

      1. Step 1: Compare Properties of Person and Employee

        Employee has all properties of Person plus salary. Person lacks salary.
      2. Step 2: Check Assignment Compatibility

        Assigning Employee instance to Person variable is valid (extra properties allowed). Assigning Person instance to Employee variable is invalid (missing salary).
      3. Final Answer:

        Only let p: Person = new Employee(); is valid because Employee has all Person properties -> Option A
      4. Quick Check:

        Extra properties allowed, missing properties not [OK]
      Hint: Assign subclass to superclass type, not vice versa [OK]
      Common Mistakes:
      • Assuming both assignments are valid
      • Confusing extra vs missing properties
      • Thinking class names affect compatibility
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard