Hone logo
Hone
Problems

Mastering String Literal Types in TypeScript

String literal types allow you to precisely define what string values a variable or parameter can accept. This enhances type safety by preventing unexpected string inputs and improving code readability by making intentions explicit. This challenge will test your ability to create and leverage these powerful types.

Problem Description

Your task is to create a set of string literal types in TypeScript to represent a predefined set of colors and a set of directions. You will then use these types to define function signatures and variable types, ensuring that only valid values can be used.

Requirements:

  1. Color Type: Create a string literal type named Color that can only be one of the following strings: "red", "green", "blue".
  2. Direction Type: Create a string literal type named Direction that can only be one of the following strings: "north", "south", "east", "west".
  3. paintWall Function: Define a function paintWall that accepts two arguments:
    • color: This argument must be of type Color.
    • pattern: This argument can be any string. The function should return a string indicating the wall's new color and pattern.
  4. move Function: Define a function move that accepts one argument:
    • direction: This argument must be of type Direction. The function should return a string indicating the direction of movement.
  5. Variable Declaration: Declare a variable favoriteColor and assign it a valid Color value.

Expected Behavior:

  • Your code should compile without type errors when valid Color and Direction values are used.
  • Attempts to assign invalid string literals to variables of type Color or Direction, or to pass them to the paintWall and move functions, should result in TypeScript compilation errors.

Examples

Example 1: Valid Usage

// Assume Color and Direction types are defined as required
// Assume paintWall and move functions are defined as required

const myWallColor: Color = "blue";
const movementDirection: Direction = "east";

console.log(paintWall("red", "stripes")); // Expected Output: "Wall painted red with stripes."
console.log(move("north"));           // Expected Output: "Moving north."

let favoriteColor: Color = "green";

Explanation: This example demonstrates the correct usage of the Color and Direction types. The paintWall and move functions are called with valid string literal arguments, and the favoriteColor variable is assigned a valid Color value.

Example 2: Invalid Usage (Demonstrating Type Safety)

// Assume Color and Direction types are defined as required
// Assume paintWall and move functions are defined as required

// This line would cause a TypeScript error:
// const invalidColor: Color = "yellow";

// This line would cause a TypeScript error:
// const invalidDirection: Direction = "up";

// This line would cause a TypeScript error:
// paintWall("purple", "dots");

// This line would cause a TypeScript error:
// move("left");

Explanation: These commented-out lines illustrate how TypeScript's type checking prevents invalid assignments and function calls. If you were to uncomment these lines in your actual code, TypeScript would flag them as errors, enforcing the constraints of the defined literal types.

Constraints

  • All string literal types must be defined using type aliases.
  • The function signatures must precisely match the types specified.
  • No runtime checks are required; the focus is solely on TypeScript's static type checking.

Notes

  • Think about how you can combine multiple string literals to form a single type.
  • Remember that string literal types are a subset of the string type.
  • The goal is to leverage TypeScript's compile-time checks to catch potential errors early in the development process.
Loading editor...
typescript