← Back to TypeScript Course | Chapter 9: Modules and Namespaces | Lesson 3 of 7

Default Exports की जानकारी

एक default export वह main value है जो एक module दूसरी files को प्रदान करता है। एक module का एक default export हो सकता है, और importing file इसके local name चुन सकती है।
Syntax
typescript
export default expression;
import anyName from "./module";

Default Export बनाना

किसी module के default export के रूप में एक declaration mark करने के लिए export default उपयोग करें, signal करते हुए कि यह वही single main चीज़ है जिसे यह file देने वाली है।

उदाहरण: Creating a Default Export

typescript
// logger.ts
export default function log(message: string) {
  console.log("[LOG]", message);
}

log("Hello from default export");

Default Export Import करना

एक default export curly braces के बिना import किया जाता है, और एक named import के विपरीत, importer इसके लिए कोई भी local name free रूप से चुन सकता है। यह flexibility convenient है, लेकिन इसका मतलब यह भी है कि imported name यह कोई automatic hint नहीं देता कि module इसे internally असल में क्या कहता है।

उदाहरण: Importing a Default Export

typescript
// logger.ts
export default function log(message: string) {
  console.log("[LOG]", message);
}

// main.ts
// import myLogger from "./logger";
const myLogger = log;
myLogger("Custom name works fine");

Default and Named Exports Together

एक module एक default export और कितने भी named exports साथ-साथ रख सकता है, एक primary export को अपने own names के under exported supporting helpers के साथ combine करते हुए।

उदाहरण: Default and Named Exports Together

typescript
// api.ts
export default function fetchData() {
  return "data";
}
export const API_VERSION = "v2";

console.log(fetchData(), API_VERSION);

Expressions Default Export करना

एक default export directly एक expression भी export कर सकता है, जैसे एक object literal या एक anonymous function value, किसी पहले से named declaration की ज़रूरत के बजाय।

उदाहरण: Default Exporting Expressions

typescript
// config.ts
export default {
  host: "localhost",
  port: 8080,
};

const config = { host: "localhost", port: 8080 };
console.log(config);

Default Exports कब उपयोग करें

Default exports convenient हैं जब एक module का एक clear main concept हो — जैसे एक single component, class, या configuration object — जिसे प्रदान करने के लिए पूरी file exist करती है।

उदाहरण: When to Use Default Exports

typescript
// Button.ts
export default class Button {
  constructor(public label: string) {}
}

const btn = new Button("Submit");
console.log(btn.label);
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. एक default import पर braces उपयोग करना, जैसे import { log } from "./logger", जब default imports में कोई braces नहीं होतीं।
  2. एक module में दो export default statements लिखना, जो allowed नहीं है।
  3. एक default import rename करना और confused होना, जब importer इसके लिए कोई भी name चुन सकता है।
🔒

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.