Hone logo
Hone
Problems

Type-Level Addition in TypeScript

Type-level programming allows us to perform operations on types as if they were values. This challenge asks you to implement a type-level addition function in TypeScript, enabling you to add two numeric types together at compile time. This is useful for creating more robust and type-safe code, particularly when dealing with generic components or calculations that depend on type information.

Problem Description

You need to create a TypeScript type Add that takes two numeric types, A and B, and returns a new type representing their sum. The Add type should effectively perform addition at the type level. This means that if A is 10 and B is 5, Add<A, B> should resolve to the type 15. The types involved are numeric types, which can be numbers or strings that can be parsed as numbers.

Key Requirements:

  • The Add type must be generic, accepting two type parameters representing the numbers to be added.
  • The resulting type must accurately reflect the sum of the input types.
  • The solution should work with both number and string representations of numbers.
  • The solution should be robust and handle potential edge cases (see below).

Expected Behavior:

The Add type should be usable in a type context to infer the sum of two numeric types.

Edge Cases to Consider:

  • Zero: Adding zero to any number should result in the original number.
  • Negative Numbers: The addition should correctly handle negative numbers.
  • String Numbers: The addition should work correctly when the input types are strings representing numbers (e.g., "10" + "5" should result in "15").
  • Large Numbers: Consider potential limitations of TypeScript's type system when dealing with extremely large numbers. While perfect precision might not be possible, the solution should handle reasonably large numbers without errors.

Examples

Example 1:

Input: Add<10, 5>
Output: 15
Explanation: Simple addition of two numbers.

Example 2:

Input: Add<"7", "3">
Output: "10"
Explanation: Addition of two string numbers.

Example 3:

Input: Add<2, 0>
Output: 2
Explanation: Adding zero should return the original number.

Example 4:

Input: Add<"-1", "5">
Output: "4"
Explanation: Addition with a negative number.

Constraints

  • The input types A and B can be either numbers or strings that can be parsed as numbers.
  • The solution must be implemented using TypeScript's type system, without relying on runtime calculations.
  • The solution should be reasonably performant; excessive recursion or complex type manipulations should be avoided.
  • The resulting type should be a valid TypeScript type.

Notes

This problem requires a good understanding of TypeScript's conditional types and potentially recursive type definitions. Consider how you can leverage existing TypeScript features to achieve type-level addition. A common approach involves using a helper type that decrements one of the numbers until it reaches zero, effectively performing subtraction and then inverting the result. Remember that TypeScript's type system has limitations, and you might not be able to handle arbitrarily large numbers with perfect precision. Focus on creating a solution that is clear, concise, and handles the common use cases effectively.

Loading editor...
typescript