Advanced OOP Patterns — Series 3

Preview — 3 of 10 questions

javascript
abstract class Base {
  abstract run(): void;
}

type Ctor<T> = new (...args: any[]) => T;

const c: Ctor<Base> = Base;
ACompiles fine — a class value always matches a construct signature for its instance type
BCompiles fine, but new c() throws at runtime
CCompile-time error — an abstract class cannot be assigned to a concrete construct signature; you need abstract new (...args: any[]) => T
DCompile-time error — Ctor<T> is invalid because new signatures may not be generic

javascript
class Point {
  constructor(public x: number, readonly y: string) {}
}

type I = InstanceType<typeof Point>;
type C = ConstructorParameters<typeof Point>;
AI is Point, C is [x: number, y: string]
BI is typeof Point, C is { x: number; y: string }
CI is Point, C is [number] — only public parameters are counted
DCompile-time error — these utilities require an explicitly declared constructor type, not a class

javascript
// noImplicitOverride: true
class Base {
  greet(): string { return "hi"; }
}

class Child extends Base {
  override greeet(): string { return "yo"; }
}
ACompiles fine — override is documentation and is not checked
BCompiles fine, and greet() on a Child returns "yo"
CCompile-time error — override may only be applied to a method that has no body
DCompile-time error — greeet is marked override but no such member exists on the base class

Sign up free to play

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