Code Organization की जानकारी
In this page:
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
// 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
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
// 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
// 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
// index.ts
export { formatDate } from "./date-utils";
export { UserService } from "./user-service";
console.log("index.ts only re-exports, no real logic lives here");
- हर चीज़ के लिए
export defaultउपयोग करना, इसलिए importers हर file में चीज़ों को अलग तरीके से rename करते हैं। - दो modules के बीच circular imports बनाना, जो पहली बार उपयोग होने पर एक value को
undefinedछोड़ सकता है। - सारे types एक ही giant file में डालना, इसलिए हर feature हर चीज़ पर depend करता है।
anyavoid करें औरunknownprefer करें ताकि values को उपयोग से पहले check करना पड़े।- Code को safe और predictable रखने के लिए type narrowing और
readonlyconsistently उपयोग करें। - 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: