← Back to TypeScript Course | Chapter 4: Interfaces | Lesson 2 of 7

Optional Properties की जानकारी

Optional properties वे properties हैं जो एक object contain कर सकता है पर करना ज़रूरी नहीं। वे property name के बाद एक question mark रखकर declare की जाती हैं।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
interface Config {
  retries?: number;
  verbose?: boolean;
}
function run(config: Config) {
  console.log(config.retries ?? 3, config.verbose ?? false);
}
run({ verbose: true });
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. profile.age पढ़ना और इसे पहले undefined के लिए check किए बिना एक number के रूप में उपयोग करना।
  2. एक property को age: number? से optional मार्क करना बजाय age?: number के।
  3. सिर्फ "age" in profile उपयोग करना और यह भूल जाना कि property present हो सकती है पर undefined हो।
🔒

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.