Hone logo
Hone
Problems

Implementing Number Literal Types in TypeScript

Number literal types in TypeScript allow you to define types that represent specific numeric values. This provides enhanced type safety and code clarity by restricting variables to only accept certain numbers, preventing accidental errors and improving maintainability. This challenge will guide you through creating and utilizing number literal types to enforce specific numeric constraints within your code.

Problem Description

The goal is to create a set of TypeScript functions that leverage number literal types to ensure that input parameters are restricted to specific numeric values. You will need to define types for these specific numbers and then use them to type the function parameters. The functions should perform simple operations based on the input number, demonstrating the use of these literal types.

What needs to be achieved:

  • Define number literal types for the values 1, 2, and 3.
  • Create three functions: doubleOne, doubleTwo, and doubleThree.
  • Each function should accept a parameter of the corresponding number literal type (e.g., doubleOne accepts a parameter of type 1).
  • Each function should return the input number multiplied by 2.
  • Ensure that the TypeScript compiler enforces the type constraints, preventing the functions from being called with incorrect arguments.

Key Requirements:

  • Use number literal types to define the parameter types for the functions.
  • The functions must return the correct result (input number multiplied by 2).
  • The code must be valid TypeScript and compile without errors.
  • Demonstrate that the type system prevents incorrect arguments from being passed to the functions.

Expected Behavior:

  • Calling doubleOne(1) should return 2.
  • Calling doubleTwo(2) should return 4.
  • Calling doubleThree(3) should return 6.
  • Attempting to call doubleOne(2) or doubleOne(3) should result in a TypeScript compiler error.
  • Similarly, incorrect arguments for doubleTwo and doubleThree should also trigger compiler errors.

Edge Cases to Consider:

  • While this problem focuses on positive integers, consider how number literal types could be extended to handle other numeric types (e.g., decimals, negative numbers) in a broader context. This is not required for this specific challenge, but it's a good thought exercise.
  • Think about how you might use these types to represent enums or a limited set of options.

Examples

Example 1:

Input: doubleOne(1)
Output: 2
Explanation: The function `doubleOne` is called with the number 1, which is the expected type. The function returns 1 * 2 = 2.

Example 2:

Input: doubleOne(2)
Output: TypeScript Compiler Error: Argument of type '2' is not assignable to parameter of type '1'.
Explanation: The function `doubleOne` expects an argument of type '1', but '2' is provided. The TypeScript compiler detects this type mismatch and generates an error.

Example 3:

Input: doubleTwo(2)
Output: 4
Explanation: The function `doubleTwo` is called with the number 2, which is the expected type. The function returns 2 * 2 = 4.

Constraints

  • The solution must be written in TypeScript.
  • The solution must define number literal types for 1, 2, and 3.
  • The solution must include the functions doubleOne, doubleTwo, and doubleThree.
  • The solution must demonstrate that the TypeScript compiler enforces the type constraints.
  • The solution should be concise and readable.

Notes

  • Number literal types are a powerful feature for enforcing type safety and improving code clarity.
  • Consider how you can use these types to represent a limited set of valid values for a variable or function parameter.
  • The compiler errors are a key indicator of success – ensure you are triggering them with incorrect inputs.
  • This problem is a good starting point for exploring more advanced type manipulation techniques in TypeScript.
Loading editor...
typescript