Challenge - 5 Problems
Interface vs Type Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of extending interfaces vs type aliases
What is the output of this TypeScript code when compiled and run in a JavaScript environment?
Typescript
interface A {
x: number;
}
type B = {
y: string;
}
interface C extends A {
z: boolean;
}
type D = B & {
z: boolean;
}
const objC: C = { x: 10, z: true };
const objD: D = { y: "hello", z: false };
console.log(objC.x, objC.z);
console.log(objD.y, objD.z);Attempts:
2 left
💡 Hint
Interfaces can extend other interfaces, and type aliases can use intersection types (&) to combine types.
✗ Incorrect
Interface C extends A, so objC has properties x and z. Type alias D combines B and an object with z, so objD has y and z. The console.log prints the values of these properties correctly.
🧠 Conceptual
intermediate1:30remaining
Choosing between interface and type alias for object shapes
Which statement best describes when to prefer an interface over a type alias for defining object shapes in TypeScript?
Attempts:
2 left
💡 Hint
Think about how interfaces and type aliases behave with extension and implementation.
✗ Incorrect
Interfaces are designed to be extended and implemented, making them ideal for object shapes that may grow. Type aliases are more flexible for unions, primitives, and complex types but cannot be implemented or reopened.
🔧 Debug
advanced1:30remaining
Why does this type alias cause an error?
This TypeScript code causes an error. What is the reason?
Typescript
type Person = {
name: string;
}
type Person = {
age: number;
};Attempts:
2 left
💡 Hint
Consider how TypeScript treats multiple declarations of the same type alias name.
✗ Incorrect
Unlike interfaces, type aliases cannot be reopened or redeclared with the same name. This code tries to declare 'Person' twice, causing a duplicate identifier error.
❓ Predict Output
advanced2:00remaining
Output of intersection types vs interface merging
What is the output of this TypeScript code when compiled and run?
Typescript
interface X {
a: number;
}
interface X {
b: string;
}
type Y = { a: number } & { b: string };
const objX: X = { a: 5, b: "hello" };
const objY: Y = { a: 5, b: "hello" };
console.log(objX.b);
console.log(objY.b);Attempts:
2 left
💡 Hint
Interfaces with the same name merge their properties automatically.
✗ Incorrect
Interfaces with the same name merge, so interface X has both a and b. The intersection type Y combines two object types with a and b. Both objX and objY have property b with value 'hello'.
🧠 Conceptual
expert1:30remaining
When does a type alias support features interfaces do not?
Which of the following features can a type alias support but an interface cannot in TypeScript?
Attempts:
2 left
💡 Hint
Think about the flexibility of type aliases with complex types.
✗ Incorrect
Type aliases can represent union and intersection types, which interfaces cannot. Interfaces support declaration merging and can be implemented by classes, but cannot represent unions.