Sub topic 5
Laying out projects in a repository
This chapter covers how to arrange directories, tsconfig.json files, and output folders so project references work without surprises. It assumes you can read a tsconfig.json, know what rootDir and outDir do, and have seen the composite option and the references field from the earlier chapters. The layout decisions here are what the later build and troubleshooting chapters rely on.
One project per tsconfig, one tsconfig per folder
A TypeScript project, in the project references sense, is one tsconfig.json and the set of files it compiles. Each project has its own inputs, its own outputs, and its own .tsbuildinfo file. The compiler treats a project as a unit that is either up to date or not.
The simplest layout follows from this. Give each project its own directory with the tsconfig.json at its top, and put its source in a src folder beside it. Here is the layout the tutorial's example uses:
repo/
package.json
tsconfig.base.json
tsconfig.json
packages/
core/
tsconfig.json
src/
greet.ts
dist/
app/
tsconfig.json
src/
main.ts
dist/There are three kinds of config file here:
tsconfig.base.jsonholds shared compiler options and is never built directly.packages/*/tsconfig.jsonare the real projects. Each one extends the base and sets its ownrootDir,outDir, andinclude.- The root
tsconfig.jsonis a solution file. It lists the projects and compiles nothing itself. A later chapter covers it in detail.
The file name tsconfig.base.json is a convention, not a rule. Any name works, as long as extends points at it.
Solid arrows are references, which affect build order. Dotted arrows are extends, which only copy options. The two relationships are independent. A project can extend a config it never builds against, and it can reference a project with a completely different base.
Keep source and output apart
Every project should write to an outDir that is separate from its source and is not matched by its own include. The reason is that a composite project must account for every file that is part of the program. If the compiler's own .d.ts output lands inside the include glob, the next build sees it as an input.
The convention that avoids this is src for inputs and dist for outputs, both inside the project directory:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}Setting rootDir explicitly matters more than it looks. With composite on, rootDir defaults to the directory that holds the tsconfig.json. Your output would then be dist/src/greet.js instead of dist/greet.js, because the src segment is preserved. Setting rootDir to src gives dist/greet.js and dist/greet.d.ts, which is what most package.json entry points expect. It also makes the compiler reject any file outside src that sneaks into the program.
Add the outputs to .gitignore:
dist/
*.tsbuildinfoWhere the .tsbuildinfo file lands depends on settings. By default it goes next to the output, or inside outDir when one is set. If you set tsBuildInfoFile yourself, make sure it ends up somewhere your ignore rules cover.
Paths in a base config are relative to the base
A common layout mistake is moving outDir or include into the base config to avoid repeating them. Relative paths in an extended config are resolved relative to the file that contains them, not the file that extends it. Consider this base:
{
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"]
}Every project that extends it now points outDir at <repo root>/dist and include at <repo root>/src. All projects would write to the same folder, and none would find their own source. Keep path-bearing options (rootDir, outDir, include, files, references, tsBuildInfoFile) in each project's own tsconfig.json. The base should hold only options that have no paths: target, module, moduleResolution, strict, skipLibCheck.
Two other details about inheritance affect layout:
referencesis never inherited throughextends. Each project declares its own.include,files, andexcludefrom the extending config replace the base values. They are not merged.
Choosing project boundaries
You could make every folder a project, but each project adds a config file and a build step, and it multiplies the declaration files the compiler reads. These are practical rules for where to draw the lines.
Make something its own project when:
- Other code depends on it, and it changes less often than its consumers. A shared library is the standard case.
- It needs different compiler options, such as a different
libfor browser code and Node code. - It has a different set of globals or types. Test code that uses
@types/jestshould not leak those globals into production code. The tests-and-tooling chapter returns to this. - It is large enough that checking it separately saves meaningful time.
Leave something inside an existing project when it is small, it is always edited together with its neighbour, or it would force you to export internals just to cross a boundary.
A project boundary is also a visibility boundary. A consumer sees only what the referenced project's declaration files expose. Anything that was only used internally in core is invisible to app. That is usually a benefit, but it means you cannot casually import a private helper from another project and have it type-check against the source.
Nesting and sibling layouts
You can lay projects out as siblings, as above, or nest them. Nesting looks like this:
packages/
core/
tsconfig.json
src/
test/
tsconfig.jsonA nested test folder with its own config is a legitimate separate project. The catch is that the outer project must not include it. With "include": ["src"] that is automatic. If the outer config said "include": ["."], the outer project would try to compile the test files too, and the same files would belong to two projects.
The rule to keep in mind is that each source file should belong to exactly one project. When a file matches the include of two projects, you get duplicate declarations, confusing errors, or builds that silently check the file twice with different options.
Connecting the layout to imports
The layout also determines how one project names another. The example project imports core with a relative path:
import { greet } from "../../core/src/greet.js";
console.log(greet("project references"));This works because core is listed in app's references. When the compiler resolves that path to a .ts file belonging to a referenced project, it substitutes the project's output .d.ts file for type checking. The .js extension in the import is required under NodeNext, even though the file on disk is .ts.
Reaching into ../../core/src is fine for a small repository, but it ties the importing code to the directory structure. The more common approach in larger repositories uses npm workspaces:
{
"name": "repo",
"private": true,
"workspaces": ["packages/*"]
}Each package then gets a package.json with a name such as @acme/core, and types and main entries that point into dist. Consumers write import { greet } from "@acme/core". Resolution goes through node_modules, which the package manager fills with a link to the package folder. The references entry is still needed, because it is what tells tsc --build about build order. Without it, the import resolves to dist/*.d.ts that may not exist yet or may be stale.
A layout checklist
Before adding references, check each project against this list:
- It has its own directory and its own
tsconfig.json. rootDirandoutDirare set in that file, not inherited.includecovers only the project's own source, and no other project's files.outDiris outside theincludepaths and listed in.gitignore.- The shared base config contains no path-based options.
- Each project's
referenceslist names only the projects it imports from.
Exercise
A teammate moves "outDir": "dist" and "include": ["src"] into tsconfig.base.json so the project configs are shorter. After the change, packages/core and packages/app both report that they found no input files, or both write to the same place. Why does this happen, and how do you fix it?
Show answer
Relative paths in an extended config are resolved against the file that declares them, which is the base file at the repository root. So outDir points at <root>/dist and include points at <root>/src for every project, and neither folder holds the project's real source. The fix is to move outDir, rootDir, and include back into each project's own tsconfig.json, where they resolve against that project's directory, and keep only path-free options such as target and strict in the base.
Exercise
With composite on and no rootDir set, packages/core/tsconfig.json builds to dist/src/greet.js. What setting changes this to dist/greet.js, and what extra check does it give you?
Show answer
Set "rootDir": "src". The compiler then maps src/greet.ts to dist/greet.js directly. It also reports an error if any file outside src ends up in the program, which catches projects that accidentally include files they should not own.