How to configure TypeScript for a monorepo
· Category: TypeScript
Short answer
Use a shared base tsconfig.json, project references for each package, and consistent path mapping to maintain type safety and fast builds across a monorepo.
Steps
- Create a
tsconfig.base.jsonwith common compiler options. - In each package, extend the base:
"extends": "../../tsconfig.base.json". - Enable
compositeanddeclarationin package tsconfigs. - Reference dependent packages in each tsconfig's
referencesarray. - Build from the root with
tsc --buildor integrate with tools like Nx or Turborepo.
Tips
- Keep
rootDirandoutDiraligned per package to prevent output files from being scattered. - Use workspace protocols (pnpm, Yarn) alongside TypeScript references for robust dependency linking.
Common issues
- Duplicate type definitions across packages cause conflicts; centralize shared types in a
typesorcorepackage. - Inconsistent
strictsettings between packages break internal API contracts.