Optional Properties की जानकारी
In this page:
interface InterfaceName {
required: type;
optional?: type;
}
Declaring Optional Properties
एक interface के अंदर किसी property name के ठीक बाद एक question mark जोड़ना, जैसे age?: number, उस property को optional बनाता है। उस interface को satisfy करने वाले objects फिर उस property को freely include या omit कर सकते हैं, दोनों ही तरीकों में बिना किसी type error को trigger किए।
उदाहरण: Declaring Optional Properties
interface Profile {
username: string;
age?: number;
}
const profile: Profile = { username: "dev123" };
console.log(profile);
Checking Optional Properties
चूंकि एक optional property बिल्कुल मौजूद ही नहीं हो सकती, इसे उपयोग करने वाले code में उपयोग होने से पहले इसके existence की जांच की जानी चाहिए, typically एक if या in operator के साथ।
इसे बिना उस check के सीधे access करना एक ऐसा type देता है जो undefined include करता है।
उदाहरण: Checking Optional Properties
interface Profile {
username: string;
age?: number;
}
const profile: Profile = { username: "dev123" };
if ("age" in profile && profile.age !== undefined) {
console.log(profile.age);
} else {
console.log("Age not set");
}
Optional Properties with Default Values
Optional properties वाला एक object accept करने वाले functions caller द्वारा छोड़ी गई किसी भी property के लिए अपनी default value supply कर सकते हैं, अक्सर destructuring with defaults उपयोग करते हुए।
यह बाकी function को एक पूरी तरह populated object के साथ काम करने देता है, चाहे caller ने असल में क्या दिया हो।
उदाहरण: Optional Properties with Default Values
interface Options {
timeout?: number;
}
function connect({ timeout = 5000 }: Options) {
console.log(`Timeout: ${timeout}`);
}
connect({});
Optional Methods
Interface methods को data properties जैसे exactly ही तरीके से optional मार्क किया जा सकता है, method name के बाद एक question mark उपयोग करके। यह उन objects को describe करने के लिए उपयोगी है जैसे event handlers जहां कुछ callback hooks support होते हैं पर mandatory नहीं हैं।
उदाहरण: Optional Methods
interface Handlers {
onSave: () => void;
onCancel?: () => void;
}
const handlers: Handlers = { onSave: () => console.log("Saved") };
handlers.onCancel?.();
handlers.onSave();
Optional Properties in Functions
Optional interface properties flexible configuration objects के लिए खासकर उपयोगी हैं, जहां एक caller को सिर्फ उन settings को specify करना है जिन्हें वह override करना चाहता है और बाकी सब को default पर छोड़ सकता है।
यह हर caller को हर single option spell out करने के लिए मजबूर करने से बचाता है।
उदाहरण: Optional Properties in Functions
interface Config {
retries?: number;
verbose?: boolean;
}
function run(config: Config) {
console.log(config.retries ?? 3, config.verbose ?? false);
}
run({ verbose: true });
profile.ageपढ़ना और इसे पहलेundefinedके लिए check किए बिना एक number के रूप में उपयोग करना।- एक property को
age: number?से optional मार्क करना बजायage?: numberके। - सिर्फ
"age" in profileउपयोग करना और यह भूल जाना कि property present हो सकती है परundefinedहो।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: