Utility Types में Partial
In this page:
type Result = Partial<TypeName>;
Basic Partial
Partial<T> एक object को T की original properties का कोई भी subset रखने देता है, हर required property को एक optional property में बदलते हुए बिना आपको पूरा type हाथ से फिर से लिखना पड़े।
उदाहरण: Basic Partial
interface Todo {
title: string;
done: boolean;
}
type PartialTodo = Partial<Todo>;
const t: PartialTodo = { title: "Buy milk" };
console.log(t);
Partial for Updates
Partial खासतौर पर उपयोगी है जब एक function किसी object की selected properties ही update करता है, क्योंकि caller को सिर्फ एक field बदलने के लिए हर single field pass करने को मजबूर नहीं किया जाना चाहिए।
उदाहरण: Partial for Updates
interface Todo {
title: string;
done: boolean;
}
function updateTodo(todo: Todo, changes: Partial<Todo>): Todo {
return { ...todo, ...changes };
}
const updated = updateTodo({ title: "Buy milk", done: false }, { done: true });
console.log(updated);
Partial with Interfaces
Partial interfaces के साथ उतना ही अच्छा काम करता है जितना type aliases के साथ, और यह हर property का original type preserve करता है सिर्फ यह बदलते हुए कि वह property required है या optional।
उदाहरण: Partial with Interfaces
interface Settings {
theme: string;
fontSize: number;
}
const patch: Partial<Settings> = { fontSize: 14 };
console.log(patch);
Partial and Type Safety
Partial supply की गई properties पर type checking नहीं हटाता — object में मौजूद कोई भी property अब भी exactly अपने original type से match करनी चाहिए, सिर्फ इसकी presence optional बन जाती है।
उदाहरण: Partial and Type Safety
interface Todo {
title: string;
done: boolean;
}
const patch: Partial<Todo> = { done: true };
console.log(typeof patch.done);
When to Use Partial
Partial उपयोग करें जब एक operation legitimately एक existing object की properties का कोई भी subset accept कर सकता हो, जैसे एक API client में एक generic patch या update function।
उदाहरण: When to Use Partial
interface User {
name: string;
email: string;
}
function patchUser(id: number, changes: Partial<User>) {
console.log("Patching user", id, changes);
}
patchUser(1, { email: "[email protected]" });
- यह मान लेना कि
Partial<T>properties कोnullबनाता है, जब यह उन्हें सिर्फ optional (संभवतःundefined) बनाता है। Partial<Todo>को एक function return type के रूप में उपयोग करना और फिरundefinedजांचे बिना fields access करना।- Partial के deep होने की उम्मीद करना, जब nested objects अपनी required properties रखते हैं।
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