Lesson 2 of 9 · 24m
Paths, rootDir, and Boundaries Without Import Spaghetti
Design import paths that encode architecture, understand what TypeScript does and does not rewrite, and enforce package boundaries before they rot.
Most large TypeScript codebases do not collapse because the compiler is slow. They collapse because imports become a crime scene: ../../../../shared/utils, private files imported across packages, and path aliases that mean different things in the editor, test runner, bundler, and production runtime. TypeScript will resolve a lot of things. Resolving them consistently everywhere is your job.
baseUrl and paths are compile-time maps
The classic setup looks simple:
{"compilerOptions": {"baseUrl": ".", "paths": {"@core/*": ["packages/core/src/*"], "@app/*": ["apps/web/src/*"]}}}This affects TypeScript module resolution. It does not automatically rewrite emitted JavaScript. If Node executes the output, Node still has to resolve those specifiers. A bundler may understand the alias. A test runner may need its own mapping. Production Node may need package imports, a loader, or compiled path rewriting.
That mismatch is the first scar tissue lesson: never approve a path alias unless you can answer how every consumer resolves it.
Prefer package boundaries over alias archaeology
Modern workspaces already have a cleaner mechanism: package names.
packages/core/package.json -> name: "@acme/core"
apps/web/package.json -> dependency: "@acme/core"
packages/core/src/index.ts -> public entrypointTypeScript can resolve workspace package names through symlinks, package exports, and project references. This gives you one boundary that package managers, bundlers, and editors can all understand. Aliases are fine for local convenience; they are weak as architecture.
rootDir catches accidental coupling
When emitted files mirror input structure, rootDir prevents silent escapes:
{"compilerOptions": {"rootDir": "src", "outDir": "dist"}, "include": ["src"]}If src/index.ts imports ../tools/generate, the common source root no longer fits under src, and TypeScript complains or widens the inferred root. That error is annoying in the moment and valuable in the long run. It tells you a runtime artifact is reaching into build tooling or another package.
Boundary enforcement needs a second tool
TypeScript types values, not organizational intent. Use ESLint dependency rules, package exports, or an import boundary tool:
export default [{
rules: {
"import/no-restricted-paths": ["error", {
zones: [{target: "./packages/app", from: "./packages/core/src/internal"}]
}]
}
}]Architecture that depends on code review memory will erode.
Field notes
- A path alias that works in
vitestbut not in emitted Node output is not a working alias. - Avoid aliases whose names shadow real package scopes unless they are intentionally backed by packages.
- Deep relative imports are often a symptom, not the disease. The missing public API is the disease.
rootDirerrors during declaration emit frequently reveal test files accidentally included in production projects.- Keep generated files outside
srcunless they are genuinely source inputs; otherwise every boundary discussion becomes muddy.
Was this lesson helpful?