Type-Safe Array Element Checker in TypeScript
This challenge focuses on creating a TypeScript function that verifies if all elements within an array conform to a specified type. Ensuring type safety across arrays is crucial for robust and maintainable TypeScript code, preventing unexpected errors and improving code clarity. Your task is to implement a function that accurately checks the type of each element and returns a boolean indicating whether the array is type-safe.
Problem Description
You need to implement a function called isArrayTypeSafe that takes two arguments:
array: An array of any type (any[]).elementType: A TypeScript type representing the expected type of each element in the array (e.g.,string,number,boolean, or a custom type).
The function should iterate through the array and verify that each element's type matches the elementType. If even a single element does not match the specified type, the function should immediately return false. If all elements match the type, the function should return true.
Key Requirements:
- The function must be written in TypeScript.
- The function must handle arrays of any type.
- The function must accurately determine if all elements conform to the provided
elementType. - The function should return a boolean value.
- The function should be efficient; avoid unnecessary iterations.
Expected Behavior:
- If the input array is empty, the function should return
true(as there are no elements to violate the type constraint). - If the input array contains at least one element that does not match the
elementType, the function should returnfalse. - If all elements in the input array match the
elementType, the function should returntrue.
Edge Cases to Consider:
- Empty arrays.
- Arrays containing
nullorundefined. Consider how these should be handled relative to theelementType. (For this challenge,nullandundefinedshould not match primitive types likestring,number, orboolean. They can matchobjectorany.) - Arrays containing mixed types.
- Complex types (e.g., objects with specific properties).
Examples
Example 1:
Input: [1, 2, 3, 4, 5]
elementType: number
Output: true
Explanation: All elements in the array are numbers.
Example 2:
Input: ["hello", "world", 123]
elementType: string
Output: false
Explanation: The array contains a number (123) which does not match the string type.
Example 3:
Input: [true, false, true]
elementType: boolean
Output: true
Explanation: All elements are booleans.
Example 4:
Input: [null, undefined]
elementType: string
Output: false
Explanation: Neither null nor undefined match the string type.
Example 5:
Input: []
elementType: number
Output: true
Explanation: The array is empty, so it is considered type-safe.
Example 6:
Input: [{name: "Alice"}, {name: "Bob"}]
elementType: {name: string}
Output: true
Explanation: All objects have a 'name' property of type string.
Example 7:
Input: [{name: "Alice"}, {age: 30}]
elementType: {name: string}
Output: false
Explanation: The second object is missing the 'name' property.
Constraints
- The input
arraycan contain any number of elements (including zero). - The
elementTypecan be any valid TypeScript type. - The function should have a time complexity of O(n), where n is the number of elements in the array.
- The function should not modify the input array.
Notes
- Consider using TypeScript's type guards to perform type checking.
typeof,instanceof, and custom type guards can be helpful. - Be mindful of how TypeScript handles
nullandundefinedwhen performing type comparisons. - Think about how to handle complex types (objects) effectively. You might need to use type predicates or other advanced TypeScript features.
- The goal is to create a robust and type-safe solution that accurately verifies the type of array elements.