Mastering Advanced TypeScript Types
Strengthen the type safety of your TypeScript code with advanced generic types, 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) towards sophisticated use of the type system. The requirement to justify each advanced type used makes it an excellent learning tool as much as a production tool.</p><p>The categorical approach (generics, utility types, discriminated unions, template literals, mapped types) ensures exhaustive exploration of TypeScript possibilities rather than stopping at the first functional solution found.</p><p>The transformation of simple unions into discriminated unions is particularly valuable because 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 GuideGlossary Terms
Similar Prompts
Learn the basics of Git for beginners
Learn Git from scratch with illustrated explanations, concrete examples, a practical workflow and a cheatsheet of essential commands.
Define a Git strategy for a team
Define a comprehensive Git strategy adapted to your team: branching model, conventions, code review and release management.
Write integration tests for an API
Create comprehensive API integration tests with database setup, authentication, CRUD and end-to-end scenarios.
Create an optimized Dockerfile for production
Create an optimized multi-stage Dockerfile for production with maximum security, lightweight image and best practices.