P
💻DeveloppementAdvancedChatGPT

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 :

[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.
100% found this useful

Personalize this prompt with Léa

Answer 3 questions and Léa tailors the prompt to your situation.

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

Improving type safety of a TypeScript projectLearning advanced typesEliminating any types in a codebase

Expected Output

Refactored TypeScript code with advanced types, pedagogical explanation of each typing choice, and 100% type-safe code.

Improve this prompt

Run this prompt through the Optimizer to strengthen its context, constraints and expected format.

Improve this prompt with the Optimizer

Comments

  • LéaAI

    Pour éviter une réponse scolaire qui force les 7 patterns sur un code fictif, ajoutez : "N'applique un type avancé que s'il résout un problème concret du code collé. Si un pattern est inutile ici, dis-le et propose un type minimaliste." Mieux : scindez en deux étapes — d'abord un audit des types, puis un refactoring ciblé des 2-3 points critiques identifiés.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementBeginnerAll AIs

Convert CSS to Tailwind

Migrate styling to Tailwind

090
💻DeveloppementIntermediateAll AIs

Refactor legacy code to modern patterns

Gradually modernizing legacy code

0100
💻DeveloppementIntermediateGemini

Gemini Prompt for Generating SQL Queries

Gemini, Google's artificial intelligence model, excels at generating SQL queries through its deep understanding of data structures and database syntax. Whether you are working with MySQL, PostgreSQL, SQL Server or SQLite, Gemini can transform your natural language descriptions into optimized and functional SQL queries. This capability is particularly valuable for developers looking to speed up their workflow, data analysts handling complex datasets, and beginners learning SQL. By providing a well-structured prompt to Gemini, you not only get the desired query but also explanations of the logic used, optimization suggestions, and alternatives based on your database management system. The prompt engineering approach allows you to guide Gemini to take into account your specific constraints: performance, readability, compatibility with a particular DBMS, or adherence to your organization's naming conventions. Discover how to formulate your prompts to get the most out of Gemini in generating SQL queries.

0112
💻DeveloppementIntermediateAll AIs

Implement secrets management

Secure credentials management

0102