← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 11 of 20

Utility Types में Required

Required<T> एक object type की हर property को required बनाता है। यह उपयोगी है जब एक type में optional properties हों लेकिन एक particular operation को उन सबकी ज़रूरत हो।
Syntax
typescript
type Result = Required<TypeName>;

Basic Required

Required<T> T की हर optional property को एक required property में convert करता है, जो effectively Partial<T> utility type की exact opposite transformation है।

उदाहरण: Basic Required

typescript
interface Todo {
  title?: string;
  done?: boolean;
}
type CompleteTodo = Required<Todo>;
const t: CompleteTodo = { title: "Buy milk", done: false };
console.log(t);

Required for Complete Objects

Required उपयोगी है जब एक function एक object expect करता है जो पहले से पूरी तरह configure हो चुका हो, compile time पर यह सुनिश्चित करते हुए कि उस point से पहले गलती से कोई optional field unset नहीं छोड़ा गया।

उदाहरण: Required for Complete Objects

typescript
interface Config {
  host?: string;
  port?: number;
}
function start(config: Required<Config>) {
  console.log(`Starting on ${config.host}:${config.port}`);
}
start({ host: "localhost", port: 8080 });

Required Preserves Types

Required सिर्फ हर property की optionality बदलता है, इसके underlying type को नहीं — एक property जो string | undefined और optional थी एक required string | undefined बनती है, एक required plain string नहीं।

उदाहरण: Required Preserves Types

typescript
interface Profile {
  bio?: string | undefined;
}
type FullProfile = Required<Profile>;
const p: FullProfile = { bio: undefined };
console.log(p);

Required with Interfaces

Required को interface types पर बिल्कुल उसी तरह apply किया जा सकता है जैसे यह object type aliases पर apply होता है, क्योंकि दोनों object shapes describe करते हैं जिन्हें Required identically transform कर सकता है।

उदाहरण: Required with Interfaces

typescript
interface Draft {
  title?: string;
  body?: string;
}
const finalPost: Required<Draft> = { title: "Hello", body: "World" };
console.log(finalPost);

When to Use Required

Required उपयोग करें जब आपकी application logic के किसी बाद वाले stage को logically हर वह property चाहिए जो एक earlier, ज़्यादा permissive stage ने optional छोड़ दी थी, जैसे एक form पूरी तरह validate हो जाने के बाद।

उदाहरण: When to Use Required

typescript
interface FormState {
  name?: string;
  email?: string;
}
function submit(data: Required<FormState>) {
  console.log("Submitting", data);
}
submit({ name: "Ravi", email: "[email protected]" });
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. Required<Config> के साथ एक function call करना और पहले वाली एक optional property छोड़ देना, जो एक error है।
  2. यह मान लेना कि Required एक property के declared type से undefined हटा देता है, जब ? हटता है लेकिन settings के आधार पर explicit | undefined अब भी रह सकता है।
  3. Required के nested optional objects पर deeply काम करने की उम्मीद करना।

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.