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

Mapped Types की जानकारी

Mapped types एक existing type की properties को transform करके नए object types बनाते हैं। वे उपयोगी हैं जब कई properties को एक ही type transformation चाहिए।
Syntax
typescript
type MappedType<T> = {
  [K in keyof T]: T[K];
};

Basic Mapped Type

एक mapped type दूसरे type की keys पर iterate कर सकता है और हर corresponding property को एक नया type assign कर सकता है, एक existing type से mechanically derived बिल्कुल नया object type produce करते हुए।

उदाहरण: Basic Mapped Type

typescript
interface Person {
  name: string;
  age: number;
}
type Flags<T> = { [K in keyof T]: boolean };
const flags: Flags<Person> = { name: true, age: false };
console.log(flags);

Readonly Mapped Types

एक mapped type एक existing type की हर property में एक साथ readonly जोड़ सकता है, जो effectively है कि TypeScript का खुद का built-in Readonly<T> utility type अंदर से कैसे implement होता है।

उदाहरण: Readonly Mapped Types

typescript
interface Person {
  name: string;
  age: number;
}
type MyReadonly<T> = { readonly [K in keyof T]: T[K] };
const p: MyReadonly<Person> = { name: "Ravi", age: 25 };
console.log(p);

Changing Property Types

Mapped types हर property की value को एक related type में transform कर सकते हैं — उदाहरण के लिए T की हर property को T[K] | undefined में बदलना, या हर एक को एक Promise में wrap करना।

उदाहरण: Changing Property Types

typescript
interface Person {
  name: string;
  age: number;
}
type Nullable<T> = { [K in keyof T]: T[K] | undefined };
const p: Nullable<Person> = { name: "Ravi", age: undefined };
console.log(p);

Using keyof in Mapped Types

keyof operator एक type की keys obtain करता है, जिस पर एक mapped type फिर [K in keyof T] से iterate कर सकता है, नए type की shape को original type के खुद के property names से सीधे जोड़ते हुए।

उदाहरण: Using keyof in Mapped Types

typescript
interface Person {
  name: string;
  age: number;
}
type Stringify<T> = { [K in keyof T]: string };
const p: Stringify<Person> = { name: "Ravi", age: "25" };
console.log(p);

Practical Mapped Types

Mapped types domain models के reusable variations बनाने के लिए उपयोगी हैं — जैसे एक base type का एक editable या nullable version — बिना हर property को manually हाथ से rewrite किए।

उदाहरण: Practical Mapped Types

typescript
interface User {
  id: number;
  email: string;
}
type Editable<T> = { [K in keyof T]?: T[K] };
const patch: Editable<User> = { email: "[email protected]" };
console.log(patch);
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. एक object type के लिए [K in T] लिखना, जब आपको इसके property names पर iterate करने के लिए [K in keyof T] चाहिए।
  2. एक mapped type से methods जोड़ने या runtime पर run होने की उम्मीद करना, जब यह सिर्फ एक type बनाता है।
  3. Value type के रूप में T[K] भूल जाना और एक fixed type उपयोग करना, जो हर property का original type खो देता है।

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.