API reference › @evolu/common › Array › arrayFrom
Call Signature
function arrayFrom<T>(iterable: Iterable<T>): readonly T[];
Defined in: packages/common/src/Array.ts:259
Better Array.from.
- Returns readonly arrays
- Accepts length directly:
arrayFrom(3, fn)instead ofArray.from({ length: 3 }, fn) - Returns existing arrays unchanged
Creating from iterables and lengths
import { assertEqual, assertSame, assertType, arrayFrom } from "@evolu/common";
const fromSet = arrayFrom(new Set([1, 2, 3]));
assertEqual(fromSet, [1, 2, 3]);
assertType<ReadonlyArray<number>, typeof fromSet>();
assertEqual(
arrayFrom(3, (i) => i * 10),
[0, 10, 20],
);
const existing: ReadonlyArray<number> = [1, 2, 3];
assertSame(arrayFrom(existing), existing);
Unlike Array.from, there's no map parameter for iterables — use
mapArray instead, or
iterator helpers
directly on iterables.
Call Signature
function arrayFrom<T>(length: number, map: (index: number) => T): readonly T[];
Defined in: packages/common/src/Array.ts:261
From length and map function.