Generic
النوع العام (Generic)
Definition
A TypeScript feature that lets you write reusable code that works with any type, specified at the call site.
ميزة TypeScript تتيح كتابة كود قابل لإعادة الاستخدام يعمل مع أي نوع، يُحدَّد عند موقع الاستدعاء.
Why It Matters
useState<Project[]>([]) — the <Project[]> part is a generic. Understanding generics helps you correctly type your state, refs, and async functions.
useState<Project[]>([]) — الجزء <Project[]> هو نوع عام. فهم الأنواع العامة يساعدك على تصنيف حالتك ومراجعك ودوالك اللاتزامنية بشكل صحيح.
Full Definition
Example Usage
“const [projects, setProjects] = useState<Project[]>([]); async function fetchDoc<T>(id: string): Promise<T | null>”
“const [projects, setProjects] = useState<Project[]>([]); async function fetchDoc<T>(id: string): Promise<T | null>”
AI Builder Tips
Avoid these mistakes when using Generic:
Using `any` instead of a generic — defeats the purpose of TypeScript
Over-constraining generics with complex extends clauses when a simpler approach works
Sign in to unlock guided AI explanations from AI Teacher.
Generate a Prompt
Copy this prompt and use it directly with any AI model — no setup needed.
Help me build a project using Generic. Explain: 1. What is Generic and why it matters 2. The core architecture and required tools 3. Step-by-step implementation plan 4. Common mistakes to avoid: Using `any` instead of a generic — defeats the purpose of TypeScript, Over-constraining generics with complex extends clauses when a simpler approach works 5. Best practices and production tips