← Back to TypeScript Course | Chapter 28: Real World Projects | Lesson 8 of 14

TypeScript का Monorepo Project

एक बड़ा TypeScript monorepo clear dependency boundaries के साथ applications और shared packages रख सकता है। Project references और workspace tooling इस structure को support करते हैं।

Core Concept

एक full monorepo project वह सब कुछ apply करता है जो एक छोटा TypeScript setup उपयोग करता है — project references, एक shared-types package, workspace tooling — एक असली multi-package application के scale पर एक build pipeline के साथ जो इसे साथ जोड़ता है।

उदाहरण: Core Concept

typescript
// packages/api (references packages/shared)
// packages/web (references packages/shared)
console.log("Project references + shared-types + workspace tooling combined");

Basic Setup

एक basic setup workspace packages को wire करता है (npm/yarn/pnpm workspaces के ज़रिए) हर package के tsconfig.json के साथ इसकी dependencies को reference करते हुए, और एक root build script (अक्सर Turborepo या Nx) builds को dependency order में orchestrate करते हुए।

उदाहरण: Basic Setup

typescript
// root: { "workspaces": ["packages/*"] }
// turbo.json orchestrates builds across packages in dependency order
console.log("Turborepo/Nx build packages in the correct dependency order");

Typed Example

एक typed example: एक packages/api package typed route handlers export करता है, packages/web packages/shared से typed API-response shapes import करता है, और दोनों workspace root पर enforced उसी TypeScript version के against build होते हैं।

उदाहरण: Typed Example

typescript
// packages/shared/index.ts
export interface ApiUser { id: number; name: string; }
// packages/web imports ApiUser from packages/shared
const user: import("./shared").ApiUser = { id: 1, name: "Ravi" } as any;
console.log("packages/api and packages/web share one TypeScript version");

Project Usage

एक असली project में, build caching (Turborepo/Nx उन packages को rebuild करना skip करते हैं जिनके inputs नहीं बदले) वह है जो एक बड़े monorepo के CI को तेज़ रखता है भले ही packages की संख्या दर्जनों में बढ़ जाए।

उदाहरण: Project Usage

typescript
// turbo run build --filter=...changed
console.log("Build caching skips rebuilding packages with unchanged inputs");

Best Practices

Workspace root पर एक single TypeScript version pin करें, हर package को अपना version specify करने देने के बजाय, क्योंकि एक monorepo में mismatched compiler versions subtly different type-checking behavior का एक frequent, hard-to-diagnose source हैं।

उदाहरण: Best Practices

typescript
// root package.json: { "devDependencies": { "typescript": "5.4.0" } }
// individual packages should NOT pin their own separate typescript version.
console.log("Pin one TypeScript version at the workspace root");
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. Project references या workspace links जोड़ना भूल जाना, इसलिए एक package अपने sibling के types नहीं देख सकता।
  2. Packages को गलत order में build करना इसलिए dependents missing या stale output उपयोग करते हैं।
  3. एक ही dependency को हर package में अलग version पर रखना, जो type mismatches cause करता है।

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.