Inheritance & Polymorphism

Preview — 3 of 10 questions

What is a key difference between interface and type for objects?

javascript
interface UserInterface { name: string }
interface UserInterface { age: number }  // declaration merging

type UserType = { name: string }
type UserType = { age: number }  // ?
AInterfaces support declaration merging (multiple declarations merge); type aliases cannot be redeclared
Btype also supports declaration merging — both work the same way
Cinterface generates runtime code; type does not
Dtype supports more features than interface — always prefer type

What does an index signature allow?

javascript
interface Dictionary {
  [key: string]: string;
}

const dict: Dictionary = {};
dict["hello"] = "world";    // ?
dict[42] = "answer";        // ?
dict.name = "Alice";        // ?
AOnly string keys work — number keys cause an error
BAll three are valid — index signatures allow any string key (and numbers coerce to strings)
COnly bracket notation works — dict.name causes an error
DIndex signatures are deprecated in favor of Record<string, string>

What does Repository<T> allow?

javascript
interface Repository<T> {
  findById(id: string): Promise<T | null>;
  findAll(): Promise<T[]>;
  save(entity: T): Promise<T>;
  delete(id: string): Promise<void>;
}

class UserRepository implements Repository<User> { ... }
class ProductRepository implements Repository<Product> { ... }
AThe interface is parameterized — T is specified at implementation time, giving type-safe operations for each specific entity
BT is replaced with any at runtime
COnly class types can be used as T
DGeneric interfaces require extends to be used

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.