Hone logo
Hone
Problems

Type-Safe Ends: Creating Arrays with Predefined Types

This challenge focuses on creating a function in TypeScript that constructs an array with a specified type, prepended and appended with elements of that same type. This is useful for scenarios where you need to build arrays with a known structure and ensure type safety throughout the process, preventing unexpected type errors. The function should be generic, allowing it to work with any type.

Problem Description

You are tasked with creating a TypeScript function called createEndsWith. This function should accept three arguments:

  1. type: The type of elements the array should contain (e.g., number, string, boolean, or a custom type).
  2. startValue: The value to prepend to the array.
  3. endValue: The value to append to the array.

The function should return a new array of the specified type, containing the startValue as the first element and the endValue as the last element. The array should also contain one element of the specified type in between the start and end values.

Key Requirements:

  • Type Safety: The function must be type-safe, ensuring that all elements in the returned array conform to the provided type.
  • Generics: Utilize generics to make the function reusable with different types.
  • Correct Array Construction: The function must correctly create an array with the specified start and end values, and a single element of the correct type in between.

Expected Behavior:

The function should return an array of the correct type, with the startValue at index 0, a single element of the specified type at index 1, and the endValue at index 2.

Edge Cases to Consider:

  • What happens if startValue and endValue are of different types than the specified type? The function should still create an array of the specified type, but the start and end values should be coerced to that type if possible. If coercion is not possible, the function should return undefined.
  • Consider the case where the type is a complex object type.

Examples

Example 1:

Input: createEndsWith<number>(10, 20, 30)
Output: [10, 0, 30]
Explanation: An array of numbers is created, with 10 as the first element, 0 (the default number value) as the middle element, and 30 as the last element.

Example 2:

Input: createEndsWith<string>("hello", "world", "!")
Output: ["hello", "", "!"]
Explanation: An array of strings is created, with "hello" as the first element, an empty string as the middle element, and "!" as the last element.

Example 3:

Input: createEndsWith<boolean>(true, false, true)
Output: [true, false, true]
Explanation: An array of booleans is created, with true as the first element, false as the middle element, and true as the last element.

Example 4:

Input: createEndsWith<MyCustomType>({id: 1}, {id: 2}, {id: 3})
Output: [ {id: 1}, {}, {id: 3} ]
Explanation: An array of MyCustomType is created, with the first and last elements being the provided objects, and an empty object as the middle element.

interface MyCustomType {
  id: number;
}

Constraints

  • The function must be written in TypeScript.
  • The function must be generic.
  • The returned array must always have a length of 3.
  • The middle element of the array should be a default value for the specified type (e.g., 0 for number, "" for string, false for boolean, {} for objects).
  • If startValue or endValue cannot be coerced to the specified type, the function should return undefined.

Notes

  • Consider using TypeScript's type inference capabilities to simplify the function's implementation.
  • Think about how to handle different types gracefully and ensure type safety.
  • The default value for the middle element should be a sensible default for the given type. For objects, an empty object {} is a reasonable default.
  • Type coercion should be attempted where possible, but if it fails, return undefined.
Loading editor...
typescript