Hone logo
Hone
Problems

Safe Index Access Utilities in TypeScript

Creating robust and type-safe code often involves accessing elements within arrays or objects using indices or keys. This challenge asks you to build TypeScript utility functions that provide safer and more expressive ways to access these elements, preventing common errors like undefined or incorrect type assumptions. These utilities will enhance code readability and maintainability by explicitly handling potential missing values.

Problem Description

You are tasked with creating two TypeScript utility functions: safeArrayAccess and safeObjectAccess.

safeArrayAccess<T>(array: T[], index: number): T | undefined

This function should take an array and an index as input. It should return the element at the given index if the index is within the bounds of the array. If the index is out of bounds (negative or greater than or equal to the array's length), it should return undefined. The function must be type-safe, preserving the type of the array elements.

safeObjectAccess<T>(object: T, key: keyof T): T[key] | undefined

This function should take an object and a key (which must be a valid key of the object's type) as input. It should return the value associated with the given key if the key exists in the object. If the key does not exist, it should return undefined. The function must be type-safe, ensuring that the return type matches the type of the value associated with the key.

Key Requirements:

  • Type Safety: The functions must be fully type-safe, leveraging TypeScript's type system to prevent errors.
  • Error Handling: Handle out-of-bounds indices for arrays and non-existent keys for objects gracefully by returning undefined.
  • Readability: The code should be clear, concise, and easy to understand.
  • Generics: Utilize generics to make the functions reusable with different types.

Examples

Example 1: safeArrayAccess

Input: safeArrayAccess([1, 2, 3], 1)
Output: 2
Explanation: The element at index 1 in the array [1, 2, 3] is 2.

Example 2: safeArrayAccess

Input: safeArrayAccess([1, 2, 3], 5)
Output: undefined
Explanation: Index 5 is out of bounds for the array [1, 2, 3].

Example 3: safeObjectAccess

Input: safeObjectAccess({ name: "Alice", age: 30 }, "name")
Output: "Alice"
Explanation: The value associated with the key "name" in the object { name: "Alice", age: 30 } is "Alice".

Example 4: safeObjectAccess

Input: safeObjectAccess({ name: "Alice", age: 30 }, "city")
Output: undefined
Explanation: The key "city" does not exist in the object { name: "Alice", age: 30 }.

Constraints

  • The input array can contain any type of elements.
  • The input object can have any properties.
  • The index for safeArrayAccess must be a non-negative integer.
  • The key for safeObjectAccess must be a valid key of the object's type (enforced by keyof T).
  • Performance should be considered, but optimization is not the primary focus. Simple and readable solutions are preferred.

Notes

  • Consider using TypeScript's built-in type guards or conditional types to enhance type safety.
  • Think about how to handle edge cases effectively.
  • The goal is to create functions that are both safe and easy to use. Avoid throwing errors; returning undefined is the preferred approach for indicating a missing value.
  • The keyof T type in safeObjectAccess is crucial for ensuring type safety when accessing object properties. It guarantees that the provided key is a valid property of the object.
Loading editor...
typescript