TypeScript theorytheory 0/50 · 0%
Tooling · hard

47. Performance-aware types & type-only imports

Keeping compile times fast and bundles lean.

Large unions, deeply recursive conditional types, and complex mapped types can slow down the compiler noticeably in big codebases. `type`-only imports/exports help bundlers safely remove type-only code from the emitted JS.

ts
import type { Token } from "./token"; // guaranteed erased, no runtime import
import { createToken, type TokenOptions } from "./token"; // mixed import

export type { Token };

`isolatedModules` (common with bundler-based TS pipelines like esbuild/swc) requires each file to be independently transpilable, which forbids some patterns (like re-exporting a type without `export type`) that only work when the whole program is type-checked together. Preferring `interface` over deeply recursive `type` aliases, and avoiding unnecessarily broad unions, both keep editor responsiveness and build times healthy on large projects.

Check your understanding

  1. 1. What guarantee does `import type { Token } from "./token"` give?

  2. 2. What does `isolatedModules` require?

  3. 3. What can slow down TypeScript's compiler significantly in large codebases?