The source iterable.
Type guard evaluated for each element. The returned value is narrowed to TFiltered when not null.
The single matching element, or null when no element satisfies predicate.
Thrown when more than one element satisfies predicate.
source is fully enumerated to confirm uniqueness of the matching element.
const numbers = [5];
const singleElement = singleOrDefault(numbers);
console.log(singleElement); // 5
const numbers2 = [1, 2, 3, 4, 5];
const singleEven = singleOrDefault(numbers2, x => x > 4);
console.log(singleEven); // 5
const empty: number[] = [];
const singleOfEmpty = singleOrDefault(empty);
console.log(singleOfEmpty); // null
const noMatch = [1, 2, 3];
const singleNoMatch = singleOrDefault(noMatch, x => x > 4);
console.log(singleNoMatch); // null
Returns the only element in the sequence or the only element that satisfies an optional predicate, or null when no such element exists.
Type of elements within the source iterable.
The single element or matching element, or null when no element satisfies the conditions.
Returns the only element that satisfies the provided type guard predicate, or
nullwhen no such element exists.