conditional-types-over-overloads

Resolve function return types with conditional types over overloads.

Updated Jul 17, 2017
One-click install
npx skills add https://github.com/luyi985/lyi-bash --skill conditional-types-over-overloads
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: conditional-types-over-overloads
Source: https://github.com/luyi985/lyi-bash/tree/main/ai/skills/conditional-types-over-overloads
Command: npx skills add https://github.com/luyi985/lyi-bash --skill conditional-types-over-overloads

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

When a function's return type depends on its input type, overloads often proliferate and become hard to maintain. Conditional types provide a single, powerful alternative that handles unions cleanly and simplifies type signatures.

Core Features & Use Cases

  • Reduces overload proliferation by using a single conditional signature.
  • Handles union input types and distributes over unions automatically.
  • Useful for type utilities and generic functions where return types hinge on inputs.

Quick Start

Define a generic function signature using a conditional type to map each input union member to its corresponding output.

Frequently Asked Questions about conditional-types-over-overloads

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define a TypeScript function return type that depends on the input type?

To define a TypeScript function return type that depends on the input type, use a conditional type within a single generic signature. This automatically maps each input union member to its corresponding output and avoids maintaining multiple overloads.

Why should I prefer conditional types over overloads in TypeScript?

You should prefer conditional types over overloads to reduce overload proliferation and maintenance overhead. A single conditional signature handles union input types cleanly, distributes over unions automatically, and improves type accuracy across multiple possible outputs.

What is the best way to handle TypeScript union inputs that return different output types?

The best way to handle TypeScript union inputs returning different outputs is a conditional return type. It automatically distributes over the input union, mapping each member to its specific output without creating multiple function overloads.

How do I create a TypeScript type utility that distributes over unions?

To create a TypeScript type utility that distributes over unions, define a generic function with a conditional return type. This ensures the utility resolves the signature correctly by processing each union member individually for accurate type mapping.

What are the limitations of using TypeScript overloads for generic type signatures?

The limitations of using TypeScript overloads include proliferation and difficult maintenance when return types hinge on inputs. Overloads struggle to handle union inputs cleanly, whereas a conditional type satisfies a single generic signature and improves type accuracy.