Skip to content

only

only<T>(iterable): T

Defined in: packages/core/src/iterable/only.ts:14

Returns the single element of an iterable, rejecting any other count.

Boundary validation, and the readable form of items.length === 1 ? items[0] : throw. Use one where an empty iterable is acceptable.

Time complexity: O(1) — at most two elements are pulled, so this is safe on an infinite iterable.

T

Iterable<T>

The iterable to read from.

T

The single element.

If the iterable is empty or holds more than one element.