0
0
Typescriptprogramming~20 mins

Interface vs type alias decision in Typescript - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface vs Type Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A10 true\nhello false
B10 true\nundefined false
C10 undefined\nhello false
Dundefined true\nhello false
Attempts:
2 left
💡 Hint
Interfaces can extend other interfaces, and type aliases can use intersection types (&) to combine types.
🧠 Conceptual
intermediate
1: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?
AUse an interface when you need to extend or implement it later; use a type alias for unions or primitives.
BUse a type alias only for classes; interfaces are for functions.
CUse a type alias when you want to extend it; interfaces cannot be extended.
DUse interfaces only for primitive types; type aliases only for objects.
Attempts:
2 left
💡 Hint
Think about how interfaces and type aliases behave with extension and implementation.
🔧 Debug
advanced
1: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;
};
AType aliases must always be declared with the keyword 'interface'.
BType aliases cannot contain more than one property.
CType aliases cannot be redeclared with the same name; this causes a duplicate identifier error.
DThe second type alias must use 'extends' keyword to add properties.
Attempts:
2 left
💡 Hint
Consider how TypeScript treats multiple declarations of the same type alias name.
Predict Output
advanced
2: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);
Ahello\nundefined
B5\nhello
Cundefined\nhello
Dhello\nhello
Attempts:
2 left
💡 Hint
Interfaces with the same name merge their properties automatically.
🧠 Conceptual
expert
1: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?
AExtending multiple interfaces
BUnion and intersection types
CDeclaration merging
DImplementing by classes
Attempts:
2 left
💡 Hint
Think about the flexibility of type aliases with complex types.