Mapped Types की जानकारी
In this page:
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
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
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
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
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
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);
- एक object type के लिए
[K in T]लिखना, जब आपको इसके property names पर iterate करने के लिए[K in keyof T]चाहिए। - एक mapped type से methods जोड़ने या runtime पर run होने की उम्मीद करना, जब यह सिर्फ एक type बनाता है।
- Value type के रूप में
T[K]भूल जाना और एक fixed type उपयोग करना, जो हर property का original type खो देता है।
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