← Back to TypeScript Course | Chapter 23: Performance and Best Practices | Lesson 7 of 7

Code Organization की जानकारी

Good TypeScript organization related responsibilities को अलग करता है और dependencies समझना आसान बनाता है। Clear module boundaries, exports, और naming projects को उनके बढ़ने के साथ maintainable रहने में help करते हैं।

Responsibility से Organize करें

Files को feature या responsibility से group करना (जैसे एक users/ folder इसके types, logic, और tests के साथ) file type से group करने से बेहतर scale करता है, क्योंकि related changes types/, services/, और components/ folders के across scattered होने के बजाय co-located रहते हैं।

उदाहरण: Organize by Responsibility

typescript
// users/types.ts, users/service.ts, users/user.test.ts
// grouped together rather than scattered across types/, services/, tests/
console.log("Feature-based folders keep related files co-located");

Named Exports उपयोग करें

Named exports import site पर यह clear करते हैं कि exactly क्या pull किया जा रहा है और refactoring को safer बनाते हैं, क्योंकि एक default export rename करना इसके call sites पर कोई warning trigger नहीं करता जैसे एक named export rename करना करता है।

उदाहरण: Use Named Exports

typescript
export function formatDate(date: Date): string {
  return date.toISOString();
}
export const APP_NAME = "MyApp";
console.log(formatDate(new Date()), APP_NAME);

जब Useful हो Implementation से Types अलग करें

एक module के types को इनकी खुद की file में separate करना तब sense बनाता है जब कई files को implementation साथ लिए बिना सिर्फ shape import करना हो, unnecessary coupling avoid करते हुए।

उदाहरण: Separate Types from Implementation When Useful

typescript
// user-types.ts
interface User { id: number; name: string; }
// user-service.ts would import just the User type from user-types.ts
const u: User = { id: 1, name: "Ravi" };
console.log(u);

Circular Dependencies Avoid करें

Circular dependencies — जहां module A module B से import करता है जो वापस A से import करता है — evaluation order की वजह से values को import time पर undefined होने का कारण बन सकते हैं, इसलिए imports को एक one-directional graph के रूप में structure करना hard-to-debug bugs की एक पूरी class avoid करता है।

उदाहरण: Avoid Circular Dependencies

typescript
// a.ts imports from b.ts, b.ts imports from a.ts -> undefined at import time
// Fix: extract shared code into a third module both can import from.
console.log("Structure imports as a one-directional graph");

Entry Points छोटे रखें

एक entry point file (जैसे index.ts) को actual logic के बजाय re-exports और wiring तक limited रखना एक module की पूरी public surface को implementation details पढ़े बिना एक नज़र में देखना आसान बनाता है।

उदाहरण: Keep Entry Points Small

typescript
// index.ts
export { formatDate } from "./date-utils";
export { UserService } from "./user-service";
console.log("index.ts only re-exports, no real logic lives here");
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. हर चीज़ के लिए export default उपयोग करना, इसलिए importers हर file में चीज़ों को अलग तरीके से rename करते हैं।
  2. दो modules के बीच circular imports बनाना, जो पहली बार उपयोग होने पर एक value को undefined छोड़ सकता है।
  3. सारे types एक ही giant file में डालना, इसलिए हर feature हर चीज़ पर depend करता है।
चैप्टर सारांश
  • any avoid करें और unknown prefer करें ताकि values को उपयोग से पहले check करना पड़े।
  • Code को safe और predictable रखने के लिए type narrowing और readonly consistently उपयोग करें।
  • Interfaces और type aliases के बीच deliberately चुनें, और naming conventions और clear code organization follow करें।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.