Hone logo
Hone
Problems

Building Robust Data Structures with Discriminated Unions in TypeScript

Discriminated unions, also known as tagged unions, are a powerful feature in TypeScript that allow you to represent a value that can be one of several distinct types. This approach enhances type safety and simplifies code logic when dealing with data that can take on different forms. This challenge will guide you in creating and utilizing discriminated unions to model different states of a user interface element.

Problem Description

You are tasked with creating a discriminated union to represent the possible states of a UI component, specifically a "LoadingIndicator". This component can be in one of three states: Loading, Success, or Error. Each state carries different data: Loading has a message (string), Success has a data (any), and Error has an error (string).

Your goal is to define a TypeScript type LoadingIndicatorState as a discriminated union that encompasses these three states. You should then create a function renderLoadingIndicator that takes a LoadingIndicatorState as input and returns a string representation of the UI element based on its current state. The function should handle each state appropriately, displaying the relevant information.

Key Requirements:

  • Define the LoadingIndicatorState discriminated union with the three states: Loading, Success, and Error.
  • Each state must have a unique discriminator property (a string property that distinguishes it from other states).
  • Each state must have its own specific data properties as described above.
  • Implement the renderLoadingIndicator function that correctly renders the UI based on the state.
  • The function should return a string.

Expected Behavior:

  • When the state is Loading, the function should return a string containing the loading message.
  • When the state is Success, the function should return a string indicating success and displaying the data.
  • When the state is Error, the function should return a string indicating an error and displaying the error message.

Edge Cases to Consider:

  • Ensure the function handles all three states correctly.
  • Consider how to handle potentially empty or null data within each state. (For simplicity, assume data is always valid in this challenge).

Examples

Example 1:

Input: { type: "Loading", message: "Fetching data..." }
Output: "Loading: Fetching data..."
Explanation: The state is 'Loading', so the function returns a string containing the loading message.

Example 2:

Input: { type: "Success", data: { name: "John Doe", age: 30 } }
Output: "Success: Data: { name: 'John Doe', age: 30 }"
Explanation: The state is 'Success', so the function returns a string indicating success and displaying the data.

Example 3:

Input: { type: "Error", error: "Failed to load data." }
Output: "Error: Failed to load data."
Explanation: The state is 'Error', so the function returns a string indicating an error and displaying the error message.

Constraints

  • The type property must be a string and act as the discriminator.
  • The renderLoadingIndicator function must be implemented in TypeScript.
  • The returned string should be a clear and concise representation of the UI element's state.
  • The function should not throw any errors.

Notes

  • Think about how to use type guards (e.g., isLoading, isSuccess, isError) to safely access the properties of each state within the renderLoadingIndicator function. These guards will allow TypeScript to narrow the type of the state argument within the function.
  • Consider using template literals for string interpolation to create the output strings.
  • This challenge focuses on the core concept of discriminated unions. Error handling and more complex UI rendering logic are beyond the scope of this exercise.
Loading editor...
typescript