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:
- Color Type: Create a string literal type named
Colorthat can only be one of the following strings:"red","green","blue". - Direction Type: Create a string literal type named
Directionthat can only be one of the following strings:"north","south","east","west". paintWallFunction: Define a functionpaintWallthat accepts two arguments:color: This argument must be of typeColor.pattern: This argument can be any string. The function should return a string indicating the wall's new color and pattern.
moveFunction: Define a functionmovethat accepts one argument:direction: This argument must be of typeDirection. The function should return a string indicating the direction of movement.
- Variable Declaration: Declare a variable
favoriteColorand assign it a validColorvalue.
Expected Behavior:
- Your code should compile without type errors when valid
ColorandDirectionvalues are used. - Attempts to assign invalid string literals to variables of type
ColororDirection, or to pass them to thepaintWallandmovefunctions, 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
typealiases. - 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
stringtype. - The goal is to leverage TypeScript's compile-time checks to catch potential errors early in the development process.