API reference@evolu/commonArray › arrayFrom

Call Signature

function arrayFrom<T>(iterable: Iterable<T>): readonly T[];

Defined in: packages/common/src/Array.ts:208

Better Array.from.

  • Returns readonly arrays
  • Accepts length directly: arrayFrom(3, fn) instead of Array.from({ length: 3 }, fn)
  • Returns existing arrays unchanged

Creating from iterables and lengths

import { arrayFrom } from "@evolu/common";

const fromSet = arrayFrom(new Set([1, 2, 3]));
expect(fromSet).toEqual([1, 2, 3]);
expectTypeOf(fromSet).toEqualTypeOf<ReadonlyArray<number>>();

expect(arrayFrom(3, (i) => i * 10)).toEqual([0, 10, 20]);

const existing: ReadonlyArray<number> = [1, 2, 3];
expect(arrayFrom(existing)).toBe(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:210

From length and map function.