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

Utility Types में Partial

Partial<T> एक object type की सभी properties को optional बनाता है। यह आमतौर पर update objects और configuration overrides के लिए उपयोग होता है।
Syntax
typescript
type Result = Partial<TypeName>;

Basic Partial

Partial<T> एक object को T की original properties का कोई भी subset रखने देता है, हर required property को एक optional property में बदलते हुए बिना आपको पूरा type हाथ से फिर से लिखना पड़े।

उदाहरण: Basic Partial

typescript
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

typescript
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

typescript
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

typescript
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

typescript
interface User {
  name: string;
  email: string;
}
function patchUser(id: number, changes: Partial<User>) {
  console.log("Patching user", id, changes);
}
patchUser(1, { 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. यह मान लेना कि Partial<T> properties को null बनाता है, जब यह उन्हें सिर्फ optional (संभवतः undefined) बनाता है।
  2. Partial<Todo> को एक function return type के रूप में उपयोग करना और फिर undefined जांचे बिना fields access करना।
  3. Partial के deep होने की उम्मीद करना, जब nested objects अपनी required properties रखते हैं।

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.