Hone logo
Hone
Problems

Implementing Overload Resolution in TypeScript

TypeScript's overload resolution allows you to define multiple function signatures for the same function name, enabling the compiler to determine the correct signature based on the arguments passed. This challenge focuses on understanding and implementing overload resolution to provide type safety and improved code clarity when dealing with functions that can accept different argument types. Successfully completing this challenge demonstrates a strong grasp of TypeScript's advanced type system.

Problem Description

You are tasked with creating a function called processData that can handle different input types. The function should accept either a single number or a tuple containing a string and a number. Based on the input type, it should perform different operations and return a different type.

  • If a single number is provided: The function should square the number and return the result as a number.
  • If a tuple of [string, number] is provided: The function should concatenate the string with the number (converted to a string) and return the combined string.

The key requirement is to leverage TypeScript's overload resolution to ensure that the compiler correctly infers the return type and provides appropriate type checking based on the input arguments. You must define two function signatures (overloads) for processData to achieve this.

Expected Behavior:

  • Calling processData(5) should return 25 (a number).
  • Calling processData(["hello", 10]) should return "hello10" (a string).
  • The TypeScript compiler should not report any type errors.

Edge Cases to Consider:

  • What happens if the input is not a number or a [string, number] tuple? While the problem doesn't explicitly require error handling, consider how your code might behave in such scenarios. The focus is on overload resolution, so a simple any type for the input is acceptable for this exercise.

Examples

Example 1:

Input: processData(5)
Output: 25
Explanation: A single number (5) is provided. The function squares it (5 * 5 = 25) and returns the result.

Example 2:

Input: processData(["hello", 10])
Output: "hello10"
Explanation: A tuple containing a string and a number is provided. The function concatenates the string "hello" with the number 10 (converted to a string) and returns "hello10".

Example 3:

Input: processData([10, "world"])
Output: Type Error (Ideally, but not strictly required for this exercise)
Explanation: While the input is a tuple, the order is incorrect.  Ideally, the compiler would flag this as an error, demonstrating the effectiveness of overload resolution.

Constraints

  • The function must be named processData.
  • The function must accept either a single number or a tuple of [string, number].
  • The function must return a number when a single number is provided and a string when a tuple is provided.
  • The solution must be written in TypeScript.
  • No external libraries are allowed.

Notes

  • Think about how to define multiple function signatures for processData to represent the different input types and return types.
  • Pay close attention to the order of the overloads. TypeScript uses the order to determine which overload best matches the provided arguments.
  • Consider using type aliases to improve code readability.
  • The goal is to demonstrate understanding of overload resolution, not to create a robust error-handling system. Focus on the type signatures and how they guide the compiler.
Loading editor...
typescript