Skip to content

parseIterable

Attempts to parse a value into an iterable. This parser is strict: the value must already implement a callable [Symbol.iterator] method. No coercion is performed. It mirrors the behavior of validateIterable but returns a structured ParseResult.

It never throws and never mutates input.

Signature

function parseIterable<T = unknown>(value: unknown): ParseResult<Iterable<T>>

Parameters

Returns

  • ok: true, value: Iterable<T>, issues: []: When the value is iterable.
  • ok: false, value: null, issues: string[]: When the value is not iterable.

Behavior

  • Uses isIterable internally.
  • Accepts:
  • Arrays.
  • Strings.
  • Maps and Sets.
  • Typed arrays.
  • Generators.
  • Custom objects with a callable Symbol.iterator.
  • Rejects:
  • null and undefined.
  • Numbers, booleans.
  • Plain objects without an iterator.
  • Objects with non-callable iterators.
  • Never throws and never mutates input.

Examples

parseIterable([1, 2, 3])
// { ok: true, value: [1, 2, 3], issues: [] }

parseIterable("abc")
// { ok: true, value: "abc", issues: [] }

parseIterable({ a: 1 })
// { ok: false, value: null, issues: ["Value must be iterable"] }

parseIterable(null)
// { ok: false, value: null, issues: ["Value must be iterable"] }

Notes

  • This parser is intentionally strict and does not attempt to normalize or coerce values into iterables.
  • For coercive behavior, use normalizeIterable instead.
  • Suitable for strict protocol checks before further validation or domain logic.