Skip to content

one

one<T>(iterable): T | undefined

Defined in: packages/core/src/iterable/one.ts:18

Returns the single element of an iterable, or undefined when it is empty, rejecting any larger count.

The tolerant half of the pair only completes: both reject two or more, and only this one accepts none. T cannot itself include undefined, so the empty case is never ambiguous.

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

T extends DefinedValue

Iterable<T>

The iterable to read from.

T | undefined

The single element, or undefined if there is none.

If the iterable holds more than one element.