0
0
Typescriptprogramming~20 mins

How structural typing differs from nominal typing in Typescript - Practice Exercises

Choose your learning style9 modes available
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
intermediate
2: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);
Ax: 10, y: 20, z: 30
Bx: 10, y: 20
CType error: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'Point'.
DRuntime error: Property 'x' is undefined
Attempts:
2 left
💡 Hint
Think about how TypeScript checks object shapes rather than exact types.
Predict Output
intermediate
2: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);
ARuntime error: Property 'name' is undefined
BHello, undefined
CHello, Alice
DType error: Argument of type 'Admin' is not assignable to parameter of type 'User'.
Attempts:
2 left
💡 Hint
Consider how classes create nominal types in TypeScript.
🔧 Debug
advanced
2: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);
ABecause myTruck is undefined.
BBecause Truck does not have a model property.
CBecause showModel expects a string, not an object.
DBecause Truck and Car are different nominal types, myTruck cannot be passed to showModel.
Attempts:
2 left
💡 Hint
Think about how TypeScript treats class instances as distinct types.
🧠 Conceptual
advanced
1:30remaining
Key difference between structural and nominal typing
Which statement best describes the key difference between structural typing and nominal typing?
AStructural typing checks if types have the same shape; nominal typing checks if types have the same name.
BStructural typing requires explicit inheritance; nominal typing does not.
CNominal typing allows more flexible type assignments than structural typing.
DNominal typing ignores property names and only checks values.
Attempts:
2 left
💡 Hint
Think about how types are compared in each system.
Predict Output
expert
2: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);
ABuddy\nWhiskers
BBuddy\nType error: Argument of type 'Cat' is not assignable to parameter of type 'Animal'.
CType error: Dog is not assignable to Animal.
DRuntime error: Property 'name' is undefined
Attempts:
2 left
💡 Hint
Consider how interfaces and classes interact in TypeScript's structural typing.