CodeBlanks Logo Icon CodeBlanks

Project references

Learn multi-project setups with project references.

What are project references?

The references array lists the projects a project depends on; each entry points to a target tsconfig.json via a path field. This is used to split projects in a monorepo. Referenced projects must have composite: true.

{
  "references": [
    { "path": "../core" },
    { "path": "../utils" }
  ]
}

Build mode compiles references automatically first.

The composite requirement

If a project is referenced it must have composite: true; otherwise tsc --build errors out. Composite also forces declaration: true and outDir so the referencing project can find type declarations.

// core/tsconfig.json
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "dist"
  }
}

Referencing a non-composite project is a compile error.

Practice Resolve build order

Write a resolveBuildOrder function that returns the topological order based on each project's references. For dependencies app → core, core → utils, produce the output utils, core, app.

Starter code

index.ts
// resolveBuildOrder: references'a göre topolojik sıra verir
// her projenin references'ını takip et
// bağımlılık sırasıyla listele
// sonucu join ile yazdır

Sample solution

index.ts
function resolveBuildOrder(projects: { name: string; references: string[] }[]): string[] {
  const order: string[] = [];
  const visited = new Set<string>();

  function visit(name: string) {
    if (visited.has(name)) return;
    visited.add(name);
    const project = projects.find(p => p.name === name);
    if (project) {
      for (const ref of project.references) {
        visit(ref);
      }
    }
    order.push(name);
  }

  for (const p of projects) visit(p.name);
  return order;
}

const projects = [
  { name: "app", references: ["core"] },
  { name: "core", references: ["utils"] },
  { name: "utils", references: [] }
];
console.log(resolveBuildOrder(projects).join(", "));

Expected output

utils, core, app

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does each entry in the references array contain?

  • A type field
  • A path field Correct answer
  • A name field
  • A version field

Explanation: Each reference entry contains a path field pointing to the target tsconfig.json; name, version, and type are not part of the reference shape.

2 Add a reference path

Question
{
  "references": [
    { "____": "../core" }
  ]
}
Answer
{
  "references": [
    { "path": "../core" }
  ]
}

Explanation: Each reference entry uses a path field to point to the target tsconfig.json; name, dir, and ref are not valid fields.

3 Project references parts

Term Meaning
references lists dependent projects
path points to the target tsconfig
composite: true enables referenceability
declaration: true produces type declarations

All lessons in Expert