Hone logo
Hone
Problems

Mastering Number Literal Types in TypeScript

TypeScript's type system allows for powerful abstractions, and number literal types are a prime example. They enable you to specify exact numerical values as types, leading to more precise and safer code. This challenge will guide you through understanding and utilizing number literal types to define functions that operate on specific numerical constants.

Problem Description

Your task is to implement a function that can process different numerical constants in a type-safe manner. You will define a union type that comprises several specific number literal types. Then, you will create a function that accepts arguments of this union type and performs distinct actions based on the exact numerical value received. This will demonstrate how number literal types can be used to create highly specific and predictable behavior.

Key Requirements:

  1. Define a Union Type: Create a union type named SupportedNumbers that includes the number literal types 1, 2, and 3.
  2. Implement a Function: Create a function named processNumber that accepts one argument of type SupportedNumbers.
  3. Conditional Logic: Inside processNumber, use conditional logic (e.g., if statements or a switch statement) to check the exact value of the input number.
  4. Distinct Outputs:
    • If the input is 1, the function should return the string "One".
    • If the input is 2, the function should return the string "Two".
    • If the input is 3, the function should return the string "Three".
  5. Type Safety: Ensure that the function strictly enforces that only 1, 2, or 3 can be passed as arguments. Any other number should result in a TypeScript compilation error.

Expected Behavior:

  • Calling processNumber(1) should return "One".
  • Calling processNumber(2) should return "Two".
  • Calling processNumber(3) should return "Three".
  • Attempting to call processNumber(4) or processNumber(0) or processNumber(1.5) should cause a TypeScript error.

Edge Cases:

  • The primary edge case is ensuring that only the specified literal numbers are accepted.

Examples

Example 1:

// Input:
const result1 = processNumber(1);

// Expected Output:
// "One"

// Explanation: The input is the literal number 1, which matches the condition for returning "One".

Example 2:

// Input:
const result2 = processNumber(3);

// Expected Output:
// "Three"

// Explanation: The input is the literal number 3, which matches the condition for returning "Three".

Example 3: (Illustrating type safety)

// Input:
// const invalidResult = processNumber(5); // This line should produce a TypeScript error

// Expected Behavior:
// TypeScript compilation error: Argument of type '5' is not assignable to parameter of type 'SupportedNumbers'.
// Type '5' is not assignable to type '1'.
// Type '5' is not assignable to type '2'.
// Type '5' is not assignable to type '3'.

// Explanation: The number 5 is not part of the SupportedNumbers union type, so TypeScript prevents this invalid usage at compile time.

Constraints

  • The SupportedNumbers type must be a union of the exact literal numbers 1, 2, and 3.
  • The processNumber function must accept only arguments of type SupportedNumbers.
  • The function must return a string.
  • No runtime checks for the number values are necessary; TypeScript's static typing should handle the validation.

Notes

  • Consider how you can define your SupportedNumbers type to encapsulate these specific values.
  • Think about which control flow statement would be most appropriate for handling the distinct cases of 1, 2, and 3.
  • This exercise is about leveraging TypeScript's type system for compile-time safety and expressiveness.
Loading editor...
typescript