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.
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:
- Generic types: replace generic types with appropriate constraints (extends, conditional types) to maximize reusability without sacrificing safety.
- Utility types: apply the built-in utility types (Partial, Required, Pick, Omit, Record, ReturnType, Parameters, Awaited) where relevant.
- Discriminated unions: if applicable, turn type unions into discriminated unions for safe narrowing.
- Template literal types: use template literal types for typed strings (event names, API keys).
- Mapped types: create custom mapped types for recurring type transformations.
- Type guards: implement type guards (is, asserts) for runtime narrowing.
- 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
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 OptimizerComments
Be the first to comment on this prompt.
Glossary Terms
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Improve this prompt with the Optimizer
Analyze the prompt, strengthen the context and get a more precise version.
ChatGPT Prompts
Core techniques to guide ChatGPT more effectively.
AI prompts for web developers
Code review and debugging, keeping API keys and secrets out of the prompt.
Security, GDPR & Business Plans
ChatGPT Business, Claude Team, Gemini Workspace and Mistral Vibe compared from vendor sources: training, EU hosting and a GDPR checklist.
Similar Prompts
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.
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.
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.
Generate Storybook Stories for UI Components
Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.