← Back to TypeScript Course | Chapter 26: Monorepos and Large Projects | Lesson 3 of 5

TypeScript के Project References

Project references एक TypeScript project को दूसरे TypeScript projects पर depend करने देते हैं। वे अलग packages वाले बड़े repositories के लिए useful हैं।
Syntax
typescript
{
  "references": [{ "path": "./package-a" }],
  "files": []
}

Core Concept

Project references आपको एक बड़े TypeScript codebase को छोटे sub-projects में split करने देते हैं, हर एक का अपना tsconfig.json, जबकि tsc --build उन्हें dependency order में compile करता है और किसी भी sub-project को skip करता है जिसके inputs नहीं बदले।

उदाहरण: Core Concept

typescript
// core/tsconfig.json and app/tsconfig.json, split from one giant project
console.log("Project references split a codebase into independently built pieces");

Basic Setup

एक basic setup हर referenced sub-project के tsconfig.json में "composite": true जोड़ता है (incremental build info emit होने के लिए required) और consuming project के tsconfig.json में एक references array जो इसकी dependencies के paths की ओर point करे।

उदाहरण: Basic Setup

typescript
// core/tsconfig.json: { "compilerOptions": { "composite": true } }
// app/tsconfig.json: { "references": [{ "path": "../core" }] }
console.log("composite: true plus references wires the two projects together");

Typed Example

एक typed example: package core typed utilities export करता है, package app अपने references में { "path": "../core" } list करता है, और app के अंदर core से import करना इसके source को फिर से parse करने के बजाय core की compiled declaration files की ओर resolve होता है।

उदाहरण: Typed Example

typescript
// core exports a typed utility, app imports from "core"
// app resolves against core's compiled .d.ts, not core's raw source.
function double(x: number): number { return x * 2; }
console.log(double(5));

Project Usage

एक असली project में, project references एक slow monolithic tsc run को कई छोटे, independently cacheable builds में बदल देते हैं — जो एक codebase कुछ सौ files से बड़ा होने पर critical है।

उदाहरण: Project Usage

typescript
// tsc --build builds only what changed, in dependency order
console.log("References turn one slow tsc run into many small cacheable ones");

Best Practices

Project references को हमेशा "composite": true के साथ pair करें और builds को tsc --build (plain tsc नहीं) के through चलाएं, क्योंकि एक composite project का declaration output वह है जो पूरे reference graph को असल में काम करने देता है।

उदाहरण: Best Practices

typescript
// Always run: tsc --build   (not plain tsc)
console.log("composite + tsc --build is what makes the reference graph work");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. composite: true के बिना एक project reference करना।
  2. एक referenced project edit करना और tsc चलाना tsc -b के बजाय, इसलिए dependents outdated output उपयोग करते हैं।
  3. दो projects के बीच circular references बनाना, जो allowed नहीं है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.