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:
- Define a Union Type: Create a union type named
SupportedNumbersthat includes the number literal types1,2, and3. - Implement a Function: Create a function named
processNumberthat accepts one argument of typeSupportedNumbers. - Conditional Logic: Inside
processNumber, use conditional logic (e.g.,ifstatements or aswitchstatement) to check the exact value of the input number. - 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".
- If the input is
- Type Safety: Ensure that the function strictly enforces that only
1,2, or3can 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)orprocessNumber(0)orprocessNumber(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
SupportedNumberstype must be a union of the exact literal numbers1,2, and3. - The
processNumberfunction must accept only arguments of typeSupportedNumbers. - 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
SupportedNumberstype to encapsulate these specific values. - Think about which control flow statement would be most appropriate for handling the distinct cases of
1,2, and3. - This exercise is about leveraging TypeScript's type system for compile-time safety and expressiveness.