<A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (
self: Iterable<A>
) => Option.Option<B>
<A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (
self: Iterable<A>
) => Option.Option<B>
<A>(predicate: (a: NoInfer<A>, i: number) => boolean): (
self: Iterable<A>
) => Option.Option<A>
<A, B>(
self: Iterable<A>,
f: (a: A, i: number) => Option.Option<B>
): Option.Option<B>
<A, B extends A>(
self: Iterable<A>,
refinement: (a: A, i: number) => a is B
): Option.Option<B>
<A>(
self: Iterable<A>,
predicate: (a: A, i: number) => boolean
): Option.Option<A>Returns the first element matching a predicate, refinement, or mapping
function, wrapped in Option.
When to use
Use to scan an iterable in iteration order and return the first selected
element or mapped value as an Option.
Details
Accepts a predicate (a, i) => boolean, a refinement, or a function
(a, i) => Option<B> for simultaneous find-and-transform. If no element
matches, this returns Option.none().
Example (Finding the first match)
import { Array } from "effect"
console.log(Array.findFirst([1, 2, 3, 4, 5], (x) => x > 3)) // Option.some(4)export const const findFirst: {
<A, B>(
f: (
a: NoInfer<A>,
i: number
) => Option.Option<B>
): (self: Iterable<A>) => Option.Option<B>
<A, B extends A>(
refinement: (
a: NoInfer<A>,
i: number
) => a is B
): (self: Iterable<A>) => Option.Option<B>
<A>(
predicate: (
a: NoInfer<A>,
i: number
) => boolean
): (self: Iterable<A>) => Option.Option<A>
<A, B>(
self: Iterable<A>,
f: (a: A, i: number) => Option.Option<B>
): Option.Option<B>
<A, B extends A>(
self: Iterable<A>,
refinement: (a: A, i: number) => a is B
): Option.Option<B>
<A>(
self: Iterable<A>,
predicate: (a: A, i: number) => boolean
): Option.Option<A>
}
Returns the first element matching a predicate, refinement, or mapping
function, wrapped in Option.
When to use
Use to scan an iterable in iteration order and return the first selected
element or mapped value as an Option.
Details
Accepts a predicate (a, i) => boolean, a refinement, or a function
(a, i) => Option<B> for simultaneous find-and-transform. If no element
matches, this returns Option.none().
Example (Finding the first match)
import { Array } from "effect"
console.log(Array.findFirst([1, 2, 3, 4, 5], (x) => x > 3)) // Option.some(4)
findFirst: {
<function (type parameter) A in <A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (self: Iterable<A>) => Option.Option<B>A, function (type parameter) B in <A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (self: Iterable<A>) => Option.Option<B>B>(f: (
a: NoInfer<A>,
i: number
) => Option.Option<B>
f: (a: NoInfer<A>a: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (self: Iterable<A>) => Option.Option<B>A>, i: numberi: number) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (self: Iterable<A>) => Option.Option<B>B>): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (self: Iterable<A>) => Option.Option<B>A>) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B>(f: (a: NoInfer<A>, i: number) => Option.Option<B>): (self: Iterable<A>) => Option.Option<B>B>
<function (type parameter) A in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>A, function (type parameter) B in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>B extends function (type parameter) A in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>A>(refinement: (a: NoInfer<A>, i: number) => a is Brefinement: (a: NoInfer<A>a: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>A>, i: numberi: number) => a: NoInfer<A>a is function (type parameter) B in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>B): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>A>) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option.Option<B>B>
<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Option.Option<A>A>(predicate: (a: NoInfer<A>, i: number) => booleanpredicate: (a: NoInfer<A>a: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Option.Option<A>A>, i: numberi: number) => boolean): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Option.Option<A>A>) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Option.Option<A>A>
<function (type parameter) A in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option.Option<B>): Option.Option<B>A, function (type parameter) B in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option.Option<B>): Option.Option<B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option.Option<B>): Option.Option<B>A>, f: (a: A, i: number) => Option.Option<B>f: (a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option.Option<B>): Option.Option<B>A, i: numberi: number) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option.Option<B>): Option.Option<B>B>): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option.Option<B>): Option.Option<B>B>
<function (type parameter) A in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>A, function (type parameter) B in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>B extends function (type parameter) A in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>A>, refinement: (a: A, i: number) => a is Brefinement: (a: Aa: function (type parameter) A in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>A, i: numberi: number) => a: Aa is function (type parameter) B in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>B): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option.Option<B>B>
<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Option.Option<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Option.Option<A>A>, predicate: (a: A, i: number) => booleanpredicate: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Option.Option<A>A, i: numberi: number) => boolean): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Option.Option<A>A>
} = import moduleIterablemoduleIterable.const findFirst: {
<A, B>(
f: (a: NoInfer<A>, i: number) => Option<B>
): (self: Iterable<A>) => Option<B>
<A, B extends A>(
refinement: (
a: NoInfer<A>,
i: number
) => a is B
): (self: Iterable<A>) => Option<B>
<A>(
predicate: (
a: NoInfer<A>,
i: number
) => boolean
): (self: Iterable<A>) => Option<A>
<A, B>(
self: Iterable<A>,
f: (a: A, i: number) => Option<B>
): Option<B>
<A, B extends A>(
self: Iterable<A>,
refinement: (a: A, i: number) => a is B
): Option<B>
<A>(
self: Iterable<A>,
predicate: (a: A, i: number) => boolean
): Option<A>
}
Returns the first element that satisfies the specified
predicate, or None if no such element exists.
Example (Finding the first match)
import { Iterable, Option } from "effect"
const numbers = [1, 3, 4, 6, 8]
const firstEven = Iterable.findFirst(numbers, (x) => x % 2 === 0)
console.log(firstEven) // Option.some(4)
const firstGreaterThan10 = Iterable.findFirst(numbers, (x) => x > 10)
console.log(firstGreaterThan10) // Option.none()
// With index
const letters = ["a", "b", "c", "d"]
const atEvenIndex = Iterable.findFirst(letters, (_, i) => i % 2 === 0)
console.log(atEvenIndex) // Option.some("a")
// Type refinement
const mixed: Array<string | number> = [1, "hello", 2, "world"]
const firstString = Iterable.findFirst(
mixed,
(x): x is string => typeof x === "string"
)
console.log(firstString) // Option.some("hello")
// Transform during search
const findSquareRoot = Iterable.findFirst([1, 4, 9, 16], (x) => {
const sqrt = Math.sqrt(x)
return Number.isInteger(sqrt) ? Option.some(sqrt) : Option.none()
})
console.log(findSquareRoot) // Option.some(1)
findFirst