Languages

TypeScript interview questions

Beta

The type system beyond annotations: narrowing, generics, utility types, and how to model real domains.

80 questions · 40 theory · 40 coding

Subtopics

  • Type annotations & inference7
  • Interfaces vs type aliases6
  • Union, intersection & literal types7
  • Narrowing & type guards8
  • Generics & constraints8
  • Utility types7
  • Mapped & conditional types6
  • Enums & const assertions6
  • unknown, any, never & strictness6
  • Declaration files & module resolution6
  • Classes, access modifiers & abstract7
  • Async types & error handling6

Sample questions

  • CodingJuniorAsync types & error handling

    Write async function safeDivide(a: number, b: number): Promise<{ ok: true; value: number } | { ok: false; error: string }> that resolves to { ok: true, value: a / b }, or, when b is 0, resolves to { ok: false, error: "Division by zero" } instead of throwing.

  • TheoryJuniorAsync types & error handling

    What does the return type of an async function always look like? Why can't an async function just return T directly?

  • CodingJuniorClasses, access modifiers & abstract

    Write a BankAccount class with a private balance starting at 0, deposit(amount: number): void, and withdraw(amount: number): boolean that subtracts amount and returns true only if the balance is sufficient (otherwise it changes nothing and returns false). Then

  • CodingJuniorClasses, access modifiers & abstract

    Write an abstract class Shape with abstract area(): number, and concrete subclasses Circle (constructed with a radius) and Rectangle (constructed with width and height). Then write totalArea(shapes: ({ kind: "circle"; radius: number } | { kind: "rectangle"; width: number; height: number })[]): number

  • TheoryJuniorClasses, access modifiers & abstract

    What do public, private and protected mean on a class member? Are they enforced at runtime?

  • CodingJuniorDeclaration files & module resolution

    Write declarationFileNameFor(sourceFile: string): string that returns the declaration-file name for a JS source file: if it already ends with .d.ts, return it unchanged; if it ends with .js, .jsx, .mjs or .cjs, replace that extension with .d.ts; otherwise, append .d.ts