Skip to content

Chapter 7 of 8

Modules & Advanced Type-Level Features

TypeScript supports ES module syntax: import { ModuleName } from './module'; pulls in named exports, import * as Module from './module'; imports a whole namespace, and exports are declared with export function func() {} for named exports or export default class {} for the default export. Re-exporting from another module is as simple as export { something } from './other';. Namespaces, declared with the namespace keyword, group related code under a common name to avoid polluting the global scope. They predate ES modules and remain useful for organization, though modern code typically prefers explicit imports.

At the type level, TypeScript offers features that go well beyond simple annotations. Type guards are functions whose return type is a type predicate, like value is string, allowing the compiler to narrow the type of a value within a conditional branch. Discriminated unions extend this by giving each variant of a union a shared literal property, called a discriminant, which switch statements can exhaustively check. Conditional types resemble ternaries at the type level, mapping one type to another based on a relation—T extends null | undefined ? never : T is the pattern behind NonNullable<T>.

Mapped types iterate over the keys of another type using keyof and in to transform properties, such as { readonly [P in keyof T]: T[P] }, which is how Readonly<T> is constructed. Built on these primitives, utility types offer shortcuts for common type transformations: Partial<T> makes every property optional, Required<T> makes them all required, Pick<T, K> selects a subset of keys, and Omit<T, K> excludes them. Template literal types take this further by constructing string literal unions from interpolated expressions—for instance, `on${'click' | 'hover'}` evaluates to "onclick" | "onhover", enabling powerful compile-time string manipulation.

All chapters
  1. 1Introduction to TypeScript & Setup
  2. 2Basic Types & Type System Fundamentals
  3. 3Functions & Collection Types
  4. 4Interfaces, Type Aliases & Object Shapes
  5. 5Classes & Object-Oriented Programming
  6. 6Generics for Reusable Code
  7. 7Modules & Advanced Type-Level Features
  8. 8Strict Mode & Interoperability with JavaScript

Drill it

Reading is not remembering. These come from the Typescript Essentials deck:

Q

What is TypeScript?

TypeScript is a statically typed superset of JavaScript developed by Microsoft that compiles to plain JavaScript code. It adds optional static typing, interface...

Q

How does TypeScript relate to JavaScript?

TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript. TypeScript code is transpiled to JavaScript, allowing it to...

Q

How do you install TypeScript?

Install TypeScript globally using npm install -g typescript or locally as a dev dependency with npm install typescript --save-dev. Verify installation with tsc...

Q

What is the TypeScript compiler command?

The TypeScript compiler is invoked with tsc. Use tsc file.ts to compile a single file or tsc to compile based on tsconfig.json.