TypeScript का Monorepo Project
In this page:
Core Concept
एक full monorepo project वह सब कुछ apply करता है जो एक छोटा TypeScript setup उपयोग करता है — project references, एक shared-types package, workspace tooling — एक असली multi-package application के scale पर एक build pipeline के साथ जो इसे साथ जोड़ता है।
उदाहरण: Core Concept
// 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
// 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
// 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
// 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
// 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");
- Project references या workspace links जोड़ना भूल जाना, इसलिए एक package अपने sibling के types नहीं देख सकता।
- Packages को गलत order में build करना इसलिए dependents missing या stale output उपयोग करते हैं।
- एक ही dependency को हर package में अलग version पर रखना, जो type mismatches cause करता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next