Utility Types में Required
In this page:
type Result = Required<TypeName>;
Basic Required
Required<T> T की हर optional property को एक required property में convert करता है, जो effectively Partial<T> utility type की exact opposite transformation है।
उदाहरण: Basic Required
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
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
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
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
interface FormState {
name?: string;
email?: string;
}
function submit(data: Required<FormState>) {
console.log("Submitting", data);
}
submit({ name: "Ravi", email: "[email protected]" });
Required<Config>के साथ एक function call करना और पहले वाली एक optional property छोड़ देना, जो एक error है।- यह मान लेना कि
Requiredएक property के declared type सेundefinedहटा देता है, जब?हटता है लेकिन settings के आधार पर explicit| undefinedअब भी रह सकता है। - Required के nested optional objects पर deeply काम करने की उम्मीद करना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates