Дар бораи намудҳои кӯмакӣ дар TypeScript гап занед.
sobes.tech AI
Ҷавоб аз AI
שימוש בסוגי עזר ב-TypeScript הוא דרך לנהל ביעילות סוגי נתונים מורכבים. סוגי עזר אלה הם סוגים מובנים שמאפשרים להמיר סוגי נתונים קיימים לסוגים חדשים, לבצע פעולות שימושיות שונות ולהגביר את הגמישות והחזרות של הקוד. בין סוגי העזר המרכזיים נמצאים:
-
Partial<Type>: יוצר סוג חדש שבו כל התכונות של
Typeהן אופציונליות.interface User { id: number; name: string; age: number; } type PartialUser = Partial<User>; // PartialUser יהיה { id?: number; name?: string; age?: number; } -
Readonly<Type>: יוצר סוג חדש שבו כל התכונות של
Typeהן לקריאה בלבד.interface Point { x: number; y: number; } type ReadonlyPoint = Readonly<Point>; // ReadonlyPoint יהיה { readonly x: number; readonly y: number; } -
Pick<Type, Keys>: יוצר סוג על ידי בחירת תכונות מסוימות
Keysמ-Type.interface Product { id: number; name: string; price: number; description: string; } type ProductSummary = Pick<Product, 'id' | 'name' | 'price'>; // ProductSummary יהיה { id: number; name: string; price: number; } -
Omit<Type, Keys>: יוצר סוג על ידי הוצאה של תכונות מסוימות
Keysמ-Type.interface Order { id: number; productId: number; quantity: number; timestamp: string; } type OrderDetails = Omit<Order, 'timestamp'>; // OrderDetails יהיה { id: number; productId: number; quantity: number; } -
Exclude<Type, ExcludedUnion>: יוצר סוג על ידי הוצאה של אלמנטים מ-
Typeשיכולים להיות משויכים ל-ExcludedUnion. משמש ל-union types.type Colors = 'red' | 'green' | 'blue' | 'yellow'; type PrimaryColors = Exclude<Colors, 'yellow'>; // PrimaryColors יהיה 'red' | 'green' | 'blue' -
Extract<Type, Union>: יוצר סוג על ידי בחירת אלמנטים מ-
Typeשיכולים להיות משויכים ל-Union.type AllAnimals = 'dog' | 'cat' | 'fish' | 'bird'; type PetAnimals = Extract<AllAnimals, 'dog' | 'cat'>; // PetAnimals יהיה 'dog' | 'cat' -
NonNullable<Type>: יוצר סוג שמוציא מ-
Typeאת הערכיםnullו-undefined.type MaybeString = string | null | undefined; type NotNullableString = NonNullable<MaybeString>; // NotNullableString יהיה string -
Record<Keys, Type>: יוצר סוג אובייקט שבו המאפיינים הם מפתחות מ-
Keysוערכים מסוגType.type CityPopulation = Record<string, number>; // CityPopulation יהיה { [key: string]: number } const population: CityPopulation = { "Москва": 12635466, "Санкт-Петербург": 5381033, }; -
Parameters<Type>: מחזיר טופס (tuple) של סוגי הפרמטרים של הפונקציה
Type.function greet(name: string, age: number): string { return `Hello, ${name}! You are ${age}.`; } type GreetParams = Parameters<typeof greet>; // GreetParams יהיה [name: string, age: number] -
ReturnType<Type>: מחזיר את סוג הערך המוחזר של הפונקציה
Type.function calculateSum(a: number, b: number): number { return a + b; } type SumResult = ReturnType<typeof calculateSum>; // SumResult יהיה number -
ConstructorParameters<Type>: מחזיר טופס (tuple) או מערך של סוגי הפרמטרים של הקונסטרוקטור של מחלקה
Type.class MyClass { constructor(name: string, id: number) {} } type MyClassParams = ConstructorParameters<typeof MyClass>; // MyClassParams יהיה [name: string, id: number] -
InstanceType<Type>: מחזיר את סוג האינסטנס של המחלקה
Type.class AnotherClass { value: string; constructor(v: string) { this.value = v; } } type AnotherClassInstance = InstanceType<typeof AnotherClass>; // AnotherClassInstance יהיה AnotherClass
שימוש בסוגי עזר עושה את הקוד בטוח יותר, קל לתחזוקה ומביע יותר, ומאפשר ניהול יעיל של סוגי נתונים מורכבים.