Challenge - 5 Problems
Master of Structural vs Nominal Typing
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of structural typing example
What is the output of this TypeScript code using structural typing?
Typescript
interface Point { x: number; y: number; }
function printPoint(p: Point) {
console.log(`x: ${p.x}, y: ${p.y}`);
}
const pointLike = { x: 10, y: 20, z: 30 };
printPoint(pointLike);Attempts:
2 left
💡 Hint
Think about how TypeScript checks object shapes rather than exact types.
✗ Incorrect
TypeScript uses structural typing, so extra properties like z do not cause errors if required properties exist.
❓ Predict Output
intermediate2:00remaining
Nominal typing behavior in TypeScript with classes
What is the output or error of this TypeScript code using nominal typing with classes?
Typescript
class User { constructor(public name: string) {} } class Admin { constructor(public name: string) {} } function greet(user: User) { console.log(`Hello, ${user.name}`); } const admin = new Admin('Alice'); greet(admin);
Attempts:
2 left
💡 Hint
Consider how classes create nominal types in TypeScript.
✗ Incorrect
Classes in TypeScript create nominal types, so an instance of Admin is not assignable to User even if they have the same properties.
🔧 Debug
advanced2:00remaining
Identify the error caused by nominal typing
Why does this TypeScript code cause a type error?
Typescript
class Car { constructor(public model: string) {} } class Truck { constructor(public model: string) {} } function showModel(vehicle: Car) { console.log(vehicle.model); } const myTruck = new Truck('Ford'); showModel(myTruck);
Attempts:
2 left
💡 Hint
Think about how TypeScript treats class instances as distinct types.
✗ Incorrect
Even though Truck and Car have the same properties, their class types are nominal and incompatible.
🧠 Conceptual
advanced1:30remaining
Key difference between structural and nominal typing
Which statement best describes the key difference between structural typing and nominal typing?
Attempts:
2 left
💡 Hint
Think about how types are compared in each system.
✗ Incorrect
Structural typing compares the shape (properties) of types, while nominal typing compares their declared names or identities.
❓ Predict Output
expert2:30remaining
Output of mixed structural and nominal typing example
What is the output of this TypeScript code that mixes structural and nominal typing?
Typescript
interface Animal {
name: string;
}
class Dog implements Animal {
constructor(public name: string) {}
}
class Cat {
constructor(public name: string) {}
}
function printName(a: Animal) {
console.log(a.name);
}
const dog = new Dog('Buddy');
const cat = new Cat('Whiskers');
printName(dog);
printName(cat);Attempts:
2 left
💡 Hint
Consider how interfaces and classes interact in TypeScript's structural typing.
✗ Incorrect
TypeScript uses structural typing for interfaces, so Cat matches Animal by shape even without explicit implementation.