Core Concepts
2 min readRapid overview
TypeScript Core Concepts
Layer TypeScript on top of solid JavaScript fundamentals. Focus on strict typing, modeling real-world data, and catching errors early.
Project setup
- Strict mode: Enable
strict,noImplicitAny,strictNullChecks,exactOptionalPropertyTypes,noUncheckedIndexedAccessfor predictable behavior. - Module resolution: Prefer ESM (
module: esnext), setmoduleResolutiontobundler/node16depending on toolchain, and configurepathsfor imports. - Emit: Keep
noEmitfor type-check-only projects or targetES2022+with bundlers handling transpilation.
Type system building blocks
- Unions/intersections: Compose shapes (
type Result = Success | Failure). Use discriminated unions for runtime safety. - Literals and enums: Prefer literal unions over enums when possible for tree shaking and type inference.
- Tuples: Model fixed-length ordered data (e.g.,
[status, data]). anyvsunknownvsnever: Default tounknownfor untrusted data; useneverfor exhaustiveness; avoidanyexcept at boundaries.
Functions and generics
- Function types: Type params and return values explicitly. Use overloads for ergonomic call sites and narrow implementations.
- Generics: Constrain (
<T extends ...>) to enable safe access; combine with utility types (Partial,Pick,Omit,Record,ReturnType). - Conditional/mapped types: Transform shapes (
Readonly<T>,NonNullable<T>, customDeepPartial).
Narrowing and control flow analysis
- Type guards:
typeof,instanceof, discriminants, custom predicates (value is Foo). - Nullish checks:
value != nullfor null/undefined,??for defaults. - Exhaustiveness:
switch+neverfallthrough checks; handledefaultintentionally.
Async and errors
- Promise shapes: Type
Promise<Result<T>>, avoidPromise<any>. UseAwaited<T>to unwrap. - Errors: Treat
unknownincatch; narrow withinstanceof Erroror custom guards. - Interop: Provide types for fetch responses, third-party libs, and
JSON.parseoutput via schemas or zod/io-ts.
Tooling and testing
- Linting: ESLint with
@typescript-eslintrules; forbid implicitany, unused vars, and unhandled promises. - Builds: Vite/tsup/esbuild for apps/libs; emit types with
tsc -p tsconfig.build.jsonwhen publishing packages. - DX tips: Enable incremental/
compositefor large projects; usetsc --noEmitin CI.
Interview-ready patterns
- Implement
Result/Optiontypes and helpers. - Write a type-safe fetch wrapper that enforces response decoding.
- Model a React component prop type with discriminated unions and shared base props.
- Express recursive types for tree data and ensure depth-safe usage.