📖 Scenario: Imagine you are building a simple system to manage different types of vehicles. Each vehicle has a name and a method to display its details. You want to understand how TypeScript checks if one class can be used where another is expected, based on their structure.
🎯 Goal: You will create two classes with similar properties and methods, then check if an instance of one class can be assigned to a variable typed as the other class. This will help you learn about type compatibility in TypeScript classes.
📋 What You'll Learn
Create a class called Car with a name property and a display() method.
Create a class called Bike with a name property and a display() method.
Create a variable called vehicle typed as Car.
Assign an instance of Bike to the vehicle variable to demonstrate type compatibility.
Print the name property of vehicle.
💡 Why This Matters
🌍 Real World
Understanding type compatibility helps when working with different but similar objects, like vehicles, users, or shapes, allowing flexible and safe code.
💼 Career
Many TypeScript jobs require knowledge of how classes and interfaces relate, so you can design clean and maintainable code that works well with different data types.
Progress0 / 4 steps
1
Create the Car class
Create a class called Car with a name property set through the constructor and a display() method that logs the text "Car: " followed by the name.
Typescript
Hint
Use class Car with a constructor to set name. The display() method should print the car's name.
2
Create the Bike class
Create a class called Bike with a name property set through the constructor and a display() method that logs the text "Bike: " followed by the name.
Typescript
Hint
Similar to Car, create Bike with the same property and method names but different console text.
3
Assign Bike instance to Car variable
Create a variable called vehicle typed as Car and assign to it a new instance of Bike with the name "Speedster".
Typescript
Hint
Declare vehicle as type Car and assign it a new Bike instance.
4
Print the vehicle name
Write a line to print the name property of the vehicle variable.
Typescript
Hint
Use console.log(vehicle.name) to print the name.
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
Step 1: Understand Type Compatibility
TypeScript checks if the properties and their types in one class instance match those expected by another class type.
Step 2: Check Class Names and Methods
Class names or method counts do not affect compatibility; only the shape (properties and types) matters.
Final Answer:
When their instances have matching properties and types -> Option C
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
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.
Step 2: Identify Incorrect Syntax
Other options misuse constructor calls or type annotations and are invalid.
Final Answer:
let a: A = new B(); -> Option B
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
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.
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".
Final Answer:
undefined -> Option A
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
Step 1: Check Property Compatibility
Class A requires property x. Class B only has property y, so B instances lack x.
Step 2: Understand TypeScript Compatibility Rules
TypeScript requires the assigned instance to have all properties of the target type. Missing x causes error.
Final Answer:
Because B does not have property x required by A -> Option D
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
Step 1: Compare Properties of Person and Employee
Employee has all properties of Person plus salary. Person lacks salary.
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).
Final Answer:
Only let p: Person = new Employee(); is valid because Employee has all Person properties -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.