Sub topic 5
Laying Out Projects in a Repository
Assume you know basic TypeScript, tsconfig.json, ES modules, and how to run tsc. This chapter focuses on repository structure, not the details of TypeScript types.
A project reference works best when each referenced project has a clear boundary. A boundary usually contains its own source files, compiler configuration, generated output, and tests. The repository root then coordinates these projects.
A practical repository structure
A small repository might use this layout:
typescript-monorepo/
├── package.json
├── tsconfig.base.json
├── tsconfig.json
└── packages/
├── core/
│ ├── package.json
│ ├── tsconfig.json
│ ├── src/
│ │ └── greet.ts
│ └── dist/
│ ├── greet.js
│ ├── greet.d.ts
│ └── tsconfig.tsbuildinfo
└── app/
├── package.json
├── tsconfig.json
├── src/
│ └── main.ts
└── dist/Here, core is a library project. app is an application project that depends on it. Each project has a separate tsconfig.json, so each project can define its own rootDir and outDir.
| Directory or file | Purpose |
|---|---|
| `packages/core/src` | TypeScript source belonging to `core` |
| `packages/core/dist` | JavaScript and declaration output from `core` |
| `tsconfig.base.json` | Compiler options shared by projects |
| Root `tsconfig.json` | List of projects built together |
Keep generated directories such as dist out of source control unless your deployment process requires them. Add them to .gitignore, but decide deliberately whether CI should recreate them.
Keep configuration close to its source
A project configuration should normally live beside that project's src directory:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The paths in this file are relative to packages/core. Therefore, rootDir: "src" means packages/core/src, and outDir: "dist" means packages/core/dist.
The include pattern should describe the complete source set for the project. With composite: true, TypeScript checks that every implementation file belongs to the project. A file accidentally placed outside src can therefore produce a configuration error instead of silently becoming part of another project.
A shared base configuration reduces duplication:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true
}
}Use the base file for repository-wide rules. Keep project-specific paths and outputs in each project configuration.
Source, output, and package boundaries
Do not place generated JavaScript beside TypeScript source unless you have a specific reason. Separate src and dist directories make imports, cleanup, and publishing easier to reason about.
For a reusable package, its package.json can point consumers at generated files:
{
"name": "@example/core",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
}
}The package boundary should expose public modules through a stable entry point such as src/index.ts. Internal files can then change without forcing every consumer to know the package's directory layout.
Visualizing the layout
Exercise
Where should a project's generated declaration files go when its source is in packages/core/src and its configuration sets outDir to dist?
Show answer
They should go under packages/core/dist, because outDir is resolved relative to packages/core/tsconfig.json.