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

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

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

Brand Comment Responses

Consistent and fast social media community management

0219
💻DeveloppementIntermediateClaude

Complete Code Review for Pull Requests

Get an exhaustive code review covering quality, performance, security, and maintainability for any language.

34561
💻DeveloppementIntermediateAll AIs

Perplexity Prompt for Generating Python Code

Perplexity AI stands out from other AI assistants with its unique ability to combine real-time web search and code generation. When it comes to producing Python code, this advantage is significant: Perplexity can rely on the latest official documentation, current best practices, and up-to-date libraries to generate reliable and modern code. Unlike a classic LLM whose knowledge is frozen at a cutoff date, Perplexity verifies its sources in real time, drastically reducing the risk of obsolete code or deprecated functions. Whether you are a developer looking to speed up your workflow, a data scientist wanting to prototype quickly, or a beginner learning Python, a well-structured prompt turns Perplexity into a true development assistant. The key lies in the precision of your request: by specifying the technical context, constraints, and expected output format, you get production-ready code rather than a generic snippet. This guide offers optimized prompts to get the most out of Perplexity in your Python projects.

0204
💻DeveloppementBeginnerAll AIs

Encryption at rest and in transit

Securing data at rest and in transit

0227