Master Advanced TypeScript Types
Strengthen your code's type safety with advanced generics, utility types, and discriminated unions.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
Tu es un expert TypeScript avec une connaissance approfondie du système de types avancés. Je dois améliorer la sécurité de type de mon code TypeScript. **Code actuel avec problèmes de typage :** ```typescript [COLLER_LE_CODE_TYPESCRIPT] ``` **Contexte :** - Version TypeScript : [EX: 5.3] - Problèmes identifiés : [EX: trop de 'any', types trop larges, manque de narrowing] - Objectif : [EX: éliminer tous les 'any', créer des types réutilisables, typer une API] Améliore le typage de ce code en : 1. **Types génériques** : remplace les types génériques avec des contraintes appropriées (extends, conditional types) pour maximiser la réutilisabilité sans perdre en sécurité. 2. **Utility types** : applique les utility types natifs (Partial, Required, Pick, Omit, Record, ReturnType, Parameters, Awaited) là où c'est pertinent. 3. **Discriminated unions** : si applicable, transforme les unions de types en discriminated unions pour un narrowing sûr. 4. **Template literal types** : utilise les template literal types pour les chaînes typées (noms d'événements, clés d'API). 5. **Mapped types** : crée des mapped types personnalisés pour les transformations de types récurrentes. 6. **Type guards** : implémente des type guards (is, asserts) pour le narrowing runtime. 7. **Explication pédagogique** : pour chaque type avancé utilisé, explique pourquoi c'est la meilleure approche dans ce contexte.
Why this prompt works
<p>This prompt is designed to go beyond basic TypeScript (interfaces and simple types) toward sophisticated use of the type system. Requesting justification for each advanced type used makes it an excellent learning tool as well as a production tool.</p><p>The category-based approach (generics, utility types, discriminated unions, template literals, mapped types) ensures exhaustive exploration of TypeScript capabilities rather than stopping at the first working solution found.</p><p>Transforming simple unions into discriminated unions is particularly valuable as it activates TypeScript's automatic narrowing, eliminating manual type assertions (as) which are a frequent source of runtime bugs masked by the compiler.</p>
Use Cases
Expected Output
Refactored TypeScript code with advanced types, pedagogical explanation of each typing choice, and 100% type-safe code.
Learn more
Check the full skill on Prompt Guide to master this technique from A to Z.
View on Prompt Guide📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Similar Prompts
Automate Your Git Commits with AI
This prompt analyzes a Git diff and automatically generates structured commit messages following project conventions, with atomic splitting if needed.
Define a Git Strategy for a Team
Define a complete Git strategy adapted to your team: branching model, conventions, code review, and release management.
Legacy Code Refactoring
Safely and incrementally refactor legacy code following SOLID principles and modern best practices.
Set Up Application Observability
Implement the three pillars of observability (logs, metrics, traces) with OpenTelemetry, Prometheus, and Grafana dashboards.