Default Exports की जानकारी
In this page:
export default expression;
import anyName from "./module";
Default Export बनाना
किसी module के default export के रूप में एक declaration mark करने के लिए export default उपयोग करें, signal करते हुए कि यह वही single main चीज़ है जिसे यह file देने वाली है।
उदाहरण: Creating a Default Export
// 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
// 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
// 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
// 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
// Button.ts
export default class Button {
constructor(public label: string) {}
}
const btn = new Button("Submit");
console.log(btn.label);
- एक default import पर braces उपयोग करना, जैसे
import { log } from "./logger", जब default imports में कोई braces नहीं होतीं। - एक module में दो
export defaultstatements लिखना, जो allowed नहीं है। - एक default import rename करना और confused होना, जब importer इसके लिए कोई भी name चुन सकता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: