Project references
Why split builds
One giant project rebuilds everything on every change. Splitting shared code, server code, and client code into linked projects lets the compiler rebuild only what changed.
Small projects also give clearer errors. A failure names the project first, so you open the right folder instead of hunting through the whole tree.
{ "compilerOptions": { "composite": true, "strict": true } }
references in tsconfig
A reference declares that one project depends on another. The app project points at the shared project, and the compiler builds shared first automatically.
References also carry types across the boundary. Imports from the shared project check against its declarations without copying files around.
{ "references": [ { "path": "shared" }, { "path": "server" } ] }
tsc --build
Build mode understands references and skips work that is already fresh. Run one command at the root and every project builds in dependency order.
Later runs are incremental. Unchanged projects are skipped in seconds while edited ones rebuild with all dependents.
tsc --build
On a clean tree the command prints nothing and writes the build info of each project; an error carries the path of the project it came from, as `shared/src/bad.ts(1,14): error TS2322: …`.
When references win
References shine once a codebase has a shared core plus two or more apps or services. Below that size a single project stays simpler and faster to explain.
Reach for them when full builds feel slow or when teams step on shared files. Split at the natural seam and keep each project focused on one job.
import { formatPrice } from "shared"; const label = formatPrice(1999); console.log(label);
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a project reference in tsconfig declare?
Answer
That one project depends on another and builds it first
The reference names the dependency, and build mode orders the projects by it.
-
What does
"composite": trueenable?Answer
The build info a project needs to be referenced
A referenced project keeps build metadata, which is what composite adds.
-
What does
tsc --builddo?Answer
It builds the projects in dependency order and skips fresh ones
Build mode follows the references and rebuilds only what changed.
-
When do project references pay off?
Answer
When a shared core serves two or more apps or services
Below that size a single project stays simpler; references split a real shared seam.
-
What does this program print?
function formatPrice(cents: number): string { return "$" + (cents / 100).toFixed(2); } console.log(formatPrice(1999));Answer
$19.99
The cents are divided by 100 and fixed to two decimals, with a dollar sign in front.
-
What does this program print?
function formatPrice(cents: number): string { return "$" + (cents / 100).toFixed(2); } console.log(formatPrice(500));Answer
$5.00
Five hundred cents are five dollars, and toFixed keeps the two zeros.
-
What does this program print?
const projects = ["shared", "server"]; console.log(projects.join(","));Answer
shared,server
The join puts the comma between the two names.
-
What does this program print?
console.log("shared".toUpperCase());Answer
SHARED
toUpperCase rewrites every letter to upper case.
-
What does the compiler say about this program?
function formatPrice(cents: number): string { return cents / 100; } console.log(formatPrice(1999));Answer
error TS2322: Type 'number' is not assignable to type 'string'.
The function promises a string and returns the number of the division.
-
What does the compiler say about this program?
const price: number = "1999"; console.log(price);Answer
error TS2322: Type 'string' is not assignable to type 'number'.
A string literal cannot be stored in a variable typed as number.
Article author: Arthur Isaev