📏 Checks if a value is array-like.
Info
A value is array-like when it has a valid length and so can be indexed from 0 to length - 1. A NodeList, a FileList, arguments, a TypedArray and a string all are, without being arrays.
Syntax
TypeScript
import { isArrayLike } from '@opentf/std'; isArrayLike(val: unknown): val is ArrayLike<unknown>
Parameters
val: The value to check.
Returns
true if the value is array-like, false otherwise.
Behavior
lengthmust be an integer between0andNumber.MAX_SAFE_INTEGER, so an object carrying an unrelatedlength— a negative, a fraction or a string — is not mistaken for a collection.Functions are excluded although they have a
length: it is their arity, not a count of elements.Anything this accepts can be passed to
Array.from.
Examples
TypeScript
isArrayLike([]) //=> true isArrayLike([1, 2, 3]) //=> true isArrayLike('abc') //=> true isArrayLike({ length: 2, 0: 'a', 1: 'b' }) //=> true isArrayLike(new Uint8Array(3)) //=> true // Not array-like isArrayLike({}) //=> false isArrayLike(new Set([1, 2])) //=> false isArrayLike({ length: -1 }) //=> false isArrayLike({ length: 1.5 }) //=> false isArrayLike((a, b) => a + b) //=> false isArrayLike(null) //=> false