TypeScript
TypeScript adds static typing, tooling, and language-level ergonomics on top of modern JavaScript.
Key Concepts
- Structural typing: compatibility determined by shape, not declarations.
- Gradual typing: opt-in annotations with inference when omitted.
- Declaration merging allows multiple definitions to extend a symbol.
- Type erasure: emitted JavaScript matches configured target without TS constructs.
Important Syntax
- Interfaces and type aliases describe object shapes.
- Generics (`function wrap(value: T): Box { ... }`).
- Union and intersection types for expressive APIs.
- Utility types: Partial, Required, Pick, Omit, ReturnType, Awaited.
- Decorators (stage 3) for metadata-driven frameworks.
- enum, const enum, and literal types for finite sets.
Type System Power Tools
- Conditional types: `T extends U ? X : Y`.
- Mapped types iterate over keys and transform them.
- Infer keyword extracts types inside conditional branches.
- Template literal types compose string unions.
- Satisfies operator validates shape without widening values.
Operators & JS Interop
- All ECMAScript operators including optional chaining `?.`, nullish coalescing `??`, logical assignment `??=`.
- Readonly modifier ensures immutable fields and tuples.
- Definite assignment assertions (!) silence checks when safe.
- Non-null assertion `value!` tells the compiler you ensured defined.
Tooling
- tsconfig.json controls module targets, strict mode, path mapping.
- tsc, swc, esbuild, and Vite compile TypeScript for browsers or Node.
- ESLint with @typescript-eslint enforces style and correctness.
- Type acquisition (Automatic Type Acquisition) fetches DefinitelyTyped packages.
Best Practices
- Enable `strict` (or at least `strictNullChecks`, `noImplicitAny`).
- Prefer type-only imports (`import type { Foo } from "./foo"`).
- Express API contracts via interfaces instead of `any`/`unknown`.
- Leverage discriminated unions for state machines and reducers.