Hone logo
Hone
Problems

Type Arithmetic in TypeScript

This challenge focuses on extending TypeScript's type system to perform basic arithmetic operations (+, -, *, /) on numeric types. The goal is to create utility types that accept two numeric types and return a new type representing the result of the specified arithmetic operation. This is useful for ensuring type safety when performing calculations and can be a building block for more complex type-level computations.

Problem Description

You are tasked with creating a set of TypeScript utility types that implement arithmetic operations at the type level. Specifically, you need to define types for addition (TypeAdd), subtraction (TypeSubtract), multiplication (TypeMultiply), and division (TypeDivide). These types should take two numeric types as input (e.g., number, 10, 2.5) and return a new type representing the result of the corresponding arithmetic operation.

Key Requirements:

  • Type Safety: The resulting type should accurately reflect the type of the arithmetic operation. For example, TypeAdd<number, 5> should result in number.
  • Numeric Types Only: The input types should be numeric. If non-numeric types are provided, the compiler should produce a type error.
  • Division by Zero: The TypeDivide type should return never if either input is 0. This represents the undefined result of division by zero.
  • Integer Division: For integer inputs, TypeDivide should return an integer type.
  • Floating-Point Division: For floating-point inputs, TypeDivide should return a floating-point type.

Expected Behavior:

The utility types should be defined using conditional types and type inference to achieve the desired behavior. The compiler should be able to infer the correct result type based on the input types.

Examples

Example 1:

Input: TypeAdd<number, 5>
Output: number
Explanation: Adding a number to 5 results in a number.

Example 2:

Input: TypeMultiply<10, 2>
Output: number
Explanation: Multiplying 10 by 2 results in a number.

Example 3:

Input: TypeDivide<number, 0>
Output: never
Explanation: Division by zero is undefined, so the result is never.

Example 4:

Input: TypeDivide<10, 2>
Output: number
Explanation: Integer division of 10 by 2 results in a number.

Example 5:

Input: TypeDivide<5.5, 2>
Output: number
Explanation: Floating-point division of 5.5 by 2 results in a number.

Constraints

  • Input Types: The input types to the utility types must be numeric (e.g., number, 10, 2.5).
  • No Runtime Execution: This is a type-level challenge. The code should not be executed at runtime. The goal is to define types that are checked by the TypeScript compiler.
  • Type Safety: The solution must be type-safe and avoid any type assertions that bypass the type checker.
  • Readability: The solution should be well-structured and easy to understand.

Notes

  • Consider using conditional types (?) to implement the arithmetic operations.
  • You can leverage TypeScript's built-in type inference capabilities to simplify the implementation.
  • Think about how to handle different numeric types (integers, floats) and ensure the correct result type is returned.
  • The never type is useful for representing impossible or undefined values.
  • This challenge is about type manipulation, not runtime calculations. Focus on defining types that accurately represent the results of arithmetic operations.
Loading editor...
typescript