0
0
Typescriptprogramming~20 mins

What structural typing means in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structural Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Structural Typing in TypeScript

Which statement best describes what structural typing means in TypeScript?

ATypes are compatible if their structures (properties and methods) match, regardless of explicit declarations.
BTypes must have the same name to be considered compatible.
CType compatibility depends on inheritance hierarchy only.
DTypes are checked only at runtime, not during compilation.
Attempts:
2 left
💡 Hint

Think about how TypeScript compares objects based on their shape, not their names.

Predict Output
intermediate
2:00remaining
Output of Structural Typing Example

What is the output of this TypeScript code?

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);
ARuntime error: Property 'x' is undefined.
Bx: 10, y: 20
CType error: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'Point'.
Dx: 10, y: 20, z: 30
Attempts:
2 left
💡 Hint

Remember TypeScript allows extra properties when passing objects if required properties exist.

🔧 Debug
advanced
2:00remaining
Why does this TypeScript code cause an error?

Consider this code snippet. Why does it cause a TypeScript error?

Typescript
interface Animal { name: string; }
interface Dog extends Animal { bark(): void; }

function makeAnimalBark(animal: Dog) {
  animal.bark();
}

const myAnimal = { name: 'Buddy' };
makeAnimalBark(myAnimal);
ANo error; structural typing allows this call.
BError because 'myAnimal' has extra properties not in 'Dog'.
CError because 'myAnimal' lacks the 'bark' method required by 'Dog'.
DError because 'Animal' and 'Dog' are unrelated types.
Attempts:
2 left
💡 Hint

Check if the object passed has all required properties and methods.

📝 Syntax
advanced
2:00remaining
Which TypeScript code correctly demonstrates structural typing?

Choose the code snippet that correctly uses structural typing to assign one object to another type.

A
type A = { id: number };
type B = { id: number; name: string };
const a: A = { id: 1 };
const b: B = a;
B
type A = { id: number };
type B = { id: number; name: string };
const b: B = { id: 1, name: 'Test' };
const a: A = b;
C
interface A { id: number; }
interface B { id: number; name: string; }
const a: A = { id: 1 };
const b: B = a;
D
interface A { id: number; }
interface B { id: number; name: string; }
const b: B = { id: 1, name: 'Test' };
const a: A = b;
Attempts:
2 left
💡 Hint

Remember that a type with more properties can be assigned to a type with fewer properties if the required ones match.

🚀 Application
expert
2:00remaining
Predict the output of this structural typing example with functions

What will be the output when running this TypeScript code?

Typescript
type FuncA = (x: number) => number;
type FuncB = (x: number, y: number) => number;

const f1: FuncA = (x) => x * 2;
const f2: FuncB = (x, y) => x + y;

const test1: FuncA = f2;
const test2: FuncB = f1;

console.log(test1(5));
console.log(test2(5, 10));
AType error at compile time assigning f1 to FuncB.
B
10
TypeError at runtime because test2 expects two arguments but f1 only uses one.
CType error at compile time assigning f2 to FuncA.
D
10
15
Attempts:
2 left
💡 Hint

Think about function parameter compatibility in structural typing.