The expected element type.
The value to be checked.
A type guard used to validate each element.
true if the value is an array and all elements satisfy
the predicate, otherwise false.
This function first checks if the value is an array, then validates each element using the provided predicate.
The predicate must be a type guard (v is T) to ensure correct
type narrowing.
An empty array returns true, since no elements violate the predicate.
| Value | Predicate | Result |
|---|---|---|
[1, 2, 3] |
isNumber |
true |
[1, 'a'] |
isNumber |
false |
[] |
isNumber |
true |
'text' |
isString |
true |
{} |
isString |
false |
This function also supports union types checking:
isArrayOf<string | number>(x, (v) => isString(v) || isNumber(v));
Determines whether the provided value is an array whose elements satisfy the given predicate.