TypeScript

Static typing, generics, narrowing, and how TypeScript relates to JavaScript in frontend interviews.

Core concepts

EasyIn progress

TypeScript cheatsheet

Narrowing, generics, utility types, and interview talking points in one reference card.

TypeScript cheatsheet

Structural typing, narrowing, and generics — common talking points in frontend interviews.

Types vs interfaces

  • interface — extendable, declaration merging for libs; familiar for OOP-shaped APIs.
  • type — unions, intersections, mapped types; use for discriminated unions and conditional types.
  • Both participate in structural typing: matching shape matters, not the name.

Narrowing

  • typeof, instanceof, truthiness checks.
  • Discriminated unions: a shared literal field (e.g. kind, status) switches types.
  • in operator for object shapes; switch (true) patterns for complex guards.

Generics

  • <T> preserves relationships between inputs and outputs (e.g. Promise<T>, Array<T>).
  • Constraints: <T extends U> so T has known fields.
  • Defaults: <T = string> when callers often omit the type argument.

Utility types

  • Partial<T>, Required<T>, Readonly<T>.
  • Pick<T, K>, Omit<T, K>, Record<K, V>.
  • Parameters<typeof fn>, ReturnType<typeof fn> for inference-driven APIs.

Modules & DTS

  • import type erases at emit — good for type-only deps and avoiding circular runtime imports.
  • Ambient declarations (.d.ts) describe untyped JS or globals.
  • satisfies checks literals against a type without widening inferred literals.

Interview pitfalls

  • any disables checking; unknown forces narrowing before use.
  • Excess property checks apply to object literals, not variables — know why assignments sometimes slip through.
  • enum runtime semantics vs string unions — trade-offs for tree-shaking and ergonomics.
MediumMastered

Discriminated unions

Model success/failure and tagged variants so control flow narrows without unsafe casts.

Discriminated unions

A shared literal field (kind) lets TypeScript narrow the union inside each branch — the same idea as Redux actions or HTTP result types.

kind:circle
radius:4
area(shape):50.265
MediumNot started

Vanilla TypeScript playground

Edit real .ts in the browser — try Result<T> patterns, constraints, and mapped types.