P
💻DeveloppementAdvancedChatGPT

Master Advanced TypeScript Types

Strengthen your code's type safety with advanced generics, utility types, and discriminated unions.

Paste in your AI

Adapt this prompt

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

You are a TypeScript expert with deep knowledge of the advanced type system. I need to improve the type safety of my TypeScript code.

Current code with typing issues:

[paste the typescript code]

Context:

  • TypeScript version: [e.g. 5.3]
  • Issues identified: [e.g. too many 'any', overly broad types, lack of narrowing]
  • Goal: [e.g. eliminate all 'any', create reusable types, type an API]

Improve the typing of this code by:

  1. Generic types: replace generic types with appropriate constraints (extends, conditional types) to maximize reusability without sacrificing safety.
  2. Utility types: apply the built-in utility types (Partial, Required, Pick, Omit, Record, ReturnType, Parameters, Awaited) where relevant.
  3. Discriminated unions: if applicable, turn type unions into discriminated unions for safe narrowing.
  4. Template literal types: use template literal types for typed strings (event names, API keys).
  5. Mapped types: create custom mapped types for recurring type transformations.
  6. Type guards: implement type guards (is, asserts) for runtime narrowing.
  7. Educational explanation: for each advanced type used, explain why it's the best approach in this context.

Personalize this prompt with Léa · AI assistant

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

Why this prompt works

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. 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. 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.

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

Be the first to comment on this prompt.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateClaude

Task brief for Claude Code: frame a feature end to end

Brief template to delegate a complete feature to Claude Code: goal, starting point, constraints, definition of done, working rules and recap format.

1681
💻DeveloppementIntermediateClaude

Write a project's CLAUDE.md: the brief Claude Code reads at every session

Turns your project description into a dense CLAUDE.md: commands, structure, conventions, pitfalls, off-limits areas and situations where the agent must ask before acting.

640
💻DeveloppementAdvancedAll AIs

Design a Secure Webhook API

Design a webhook API with event subscriptions, signed deliveries, retry limits, and a delivery log your support team can inspect.

411
💻DeveloppementIntermediateAll AIs

Generate Storybook Stories for UI Components

Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.

474