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. inoperator 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>soThas 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 typeerases at emit — good for type-only deps and avoiding circular runtime imports.- Ambient declarations (
.d.ts) describe untyped JS or globals. satisfieschecks literals against a type without widening inferred literals.
Interview pitfalls
anydisables checking;unknownforces narrowing before use.- Excess property checks apply to object literals, not variables — know why assignments sometimes slip through.
enumruntime semantics vs string unions — trade-offs for tree-shaking and ergonomics.