Inheritance & Polymorphism — Series 3

Preview — 3 of 10 questions

javascript
interface Box { a: string }
interface Box { b: number }

type Crate = { a: string };
type Crate = { b: number };
ABoth pairs are errors — a name may be declared only once
BThe interface pair merges into { a: string; b: number }; the type pair is a duplicate-identifier error
CBoth pairs merge — interface and type behave identically for object shapes
DThe interface pair is an error; the type pair merges into an intersection

javascript
class Bag {
  [key: string]: number;

  count = 0;
  label = "bag";
}
ACompiles fine — index signatures on classes apply only to dynamically added keys
BCompiles fine — declared members are exempt from a class's index signature
CCompile-time error on count — a class may not declare both an index signature and named fields
DCompile-time error on label — its type string isn't assignable to the index signature's value type number

javascript
class Result<T = string> {
  constructor(public value: T) {}
}

const a = new Result("ok");
const b = new Result(42);
const c: Result = new Result("ok");
Aa is Result<string>, b is Result<number>, c is Result<string>
BAll three are Result<string> — the default overrides inference
Cb is a compile-time error — the default type parameter pins T to string
Dc is a compile-time error — a generic class always requires an explicit type argument

Sign up free to play

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