أتقن أنواع TypeScript المتقدمة: الأنواع العامة والأنواع المساعدة والأنواع الشرطية وحراس الأنواع. الدليل الشامل للتطوير الآمن للأنواع.
# TypeScript الأنواع المتقدمة
تعلم مفاهيم TypeScript المتقدمة لبناء تطبيقات قوية وآمنة للأنواع.
## الأنواع العامة (Generics)
```typescript
// دالة عامة
function firstElement<T>(array: T[]): T | undefined {
return array[0];
}
const numbers = firstElement([1, 2, 3]); // النوع: number
const strings = firstElement(["أ", "ب"]); // النوع: string
// واجهة عامة
interface ApiResponse<T> {
data: T;
success: boolean;
message?: string;
timestamp: Date;
}
interface User {
id: string;
name: string;
email: string;
}
async function getUser(id: string): Promise<ApiResponse<User>> {
const response = await fetch(`/api/users/${id}`);
return response.json();
}
```
## الأنواع المساعدة (Utility Types)
```typescript
interface Product {
id: string;
name: string;
price: number;
description: string;
stock: number;
createdAt: Date;
}
// Partial - جميع الخصائص اختيارية
type ProductUpdate = Partial<Product>;
// Pick - خصائص محددة فقط
type ProductPreview = Pick<Product, "id" | "name" | "price">;
// Omit - استبعاد خصائص محددة
type NewProduct = Omit<Product, "id" | "createdAt">;
// Required - جميع الخصائص مطلوبة
type CompleteProduct = Required<Product>;
// Readonly - جميع الخصائص للقراءة فقط
type ImmutableProduct = Readonly<Product>;
```
## الأنواع الشرطية
```typescript
// نوع شرطي بسيط
type IsString<T> = T extends string ? true : false;
type Test1 = IsString<string>; // true
type Test2 = IsString<number>; // false
// استخراج نوع عنصر المصفوفة
type ArrayElement<T> = T extends (infer E)[] ? E : never;
type NumberElement = ArrayElement<number[]>; // number
```
## حراس الأنواع (Type Guards)
```typescript
interface Dog {
type: "dog";
bark: () => void;
}
interface Cat {
type: "cat";
meow: () => void;
}
type Pet = Dog | Cat;
// دالة حارس النوع
function isDog(pet: Pet): pet is Dog {
return pet.type === "dog";
}
function petSound(pet: Pet) {
if (isDog(pet)) {
pet.bark(); // TypeScript يعلم أن pet هو Dog
} else {
pet.meow(); // TypeScript يعلم أن pet هو Cat
}
}
```
## أنواع الخرائط (Mapped Types)
```typescript
interface Form {
username: string;
email: string;
password: string;
}
// جعل جميع الحقول قابلة للـ null
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
type NullableForm = Nullable<Form>;
// حقول مع دوال getter
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type FormGetters = Getters<Form>;
```
هذا الدليل يساعد المطورين الناطقين بالعربية على فهم نظام أنواع TypeScript بشكل كامل.This typescript prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their typescript implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For typescript projects, consider mentioning your framework version, coding style, and any specific libraries you're using.