<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)export const 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: {
<function (type parameter) A in <A, B>(f: (a: NoInfer<A>, i: number) => Option<B>): (self: Iterable<A>) => Option<B>A, function (type parameter) B in <A, B>(f: (a: NoInfer<A>, i: number) => Option<B>): (self: Iterable<A>) => Option<B>B>(f: (a: NoInfer<A>, i: number) => 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<B>): (self: Iterable<A>) => Option<B>A>, i: numberi: number) => type Option<A> = O.None<A> | O.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<B>): (self: Iterable<A>) => 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<B>): (self: Iterable<A>) => Option<B>A>) => type Option<A> = O.None<A> | O.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<B>): (self: Iterable<A>) => 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<B>A, function (type parameter) B in <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => 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<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<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<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<B>A>) => type Option<A> = O.None<A> | O.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<B>B>
<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => 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<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<A>A>) => type Option<A> = O.None<A> | O.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<A>A>
<function (type parameter) A in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option<B>): Option<B>A, function (type parameter) B in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option<B>): 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<B>): Option<B>A>, f: (a: A, i: number) => Option<B>f: (a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option<B>): Option<B>A, i: numberi: number) => type Option<A> = O.None<A> | O.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<B>): Option<B>B>): type Option<A> = O.None<A> | O.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<B>): Option<B>B>
<function (type parameter) A in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option<B>A, function (type parameter) B in <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): 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<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<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<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<B>B): type Option<A> = O.None<A> | O.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<B>B>
<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): 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<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<A>A, i: numberi: number) => boolean): type Option<A> = O.None<A> | O.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<A>A>
} = import dualdual(
2,
<function (type parameter) A in <A>(self: Iterable<A>, f: ((a: A, i: number) => boolean) | ((a: A, i: number) => Option<A>)): Option<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, f: ((a: A, i: number) => boolean) | ((a: A, i: number) => Option<A>)): Option<A>A>, f: | ((a: A, i: number) => boolean)
| ((a: A, i: number) => Option<A>)
f: ((a: Aa: function (type parameter) A in <A>(self: Iterable<A>, f: ((a: A, i: number) => boolean) | ((a: A, i: number) => Option<A>)): Option<A>A, i: numberi: number) => boolean) | ((a: Aa: function (type parameter) A in <A>(self: Iterable<A>, f: ((a: A, i: number) => boolean) | ((a: A, i: number) => Option<A>)): Option<A>A, i: numberi: number) => type Option<A> = O.None<A> | O.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>, f: ((a: A, i: number) => boolean) | ((a: A, i: number) => Option<A>)): Option<A>A>)): type Option<A> = O.None<A> | O.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>, f: ((a: A, i: number) => boolean) | ((a: A, i: number) => Option<A>)): Option<A>A> => {
let let i: numberi = 0
for (const const a: Aa of self: Iterable<A>self) {
const const o: boolean | Option<A>o = f: | ((a: A, i: number) => boolean)
| ((a: A, i: number) => Option<A>)
f(const a: Aa, let i: numberi)
if (function isBoolean(input: unknown): input is booleanChecks whether a value is a boolean.
When to use
Use when you need a Predicate guard to narrow an unknown value to a
boolean.
Details
Uses typeof input === "boolean".
Example (Guarding booleans)
import { Predicate } from "effect"
const data: unknown = true
if (Predicate.isBoolean(data)) {
console.log(data ? "yes" : "no")
}
isBoolean(const o: boolean | Option<A>o)) {
if (const o: booleano) {
return import OO.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const a: Aa)
}
} else {
if (import OO.const isSome: <A>(
self: Option<A>
) => self is Some<A>
Checks whether an Option contains a value (Some).
When to use
Use when you need to branch on a present Option before accessing .value.
Details
- Acts as a type guard, narrowing to
Some<A>
Example (Checking for Some)
import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false
isSome(const o: Option<A>o)) {
return const o: O.Some<A>const o: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
o
}
}
let i: numberi++
}
return import OO.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none()
}
)