<I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [
never
]
? (input: I) => Option.Option<Unify<A>>
: Option.Option<Unify<A>>Wraps the match result in an Option, representing an optional match.
When to use
Use to finalize a matcher when unmatched input is expected and should become
Option.none.
Details
This function ensures that the result of a matcher is wrapped in an Option,
making it easy to handle cases where no pattern matches. If a match is found,
it returns Some(value), otherwise, it returns None.
This is useful in cases where a missing match is expected and should be handled explicitly rather than throwing an error or returning a default value.
Example (Extracting a user role with Match.option)
import { Match } from "effect"
type User = { readonly role: "admin" | "editor" | "viewer" }
// Create a matcher to extract user roles
const getRole = Match.type<User>().pipe(
Match.when({ role: "admin" }, () => "Has full access"),
Match.when({ role: "editor" }, () => "Can edit content"),
Match.option // Wrap the result in an Option
)
console.log(getRole({ role: "admin" }))
// Output: { _id: 'Option', _tag: 'Some', value: 'Has full access' }
console.log(getRole({ role: "viewer" }))
// Output: { _id: 'Option', _tag: 'None' }export const const option: <I, F, R, A, Pr, Ret>(
self: Matcher<I, F, R, A, Pr, Ret>
) => [Pr] extends [never]
? (input: I) => Option.Option<Unify<A>>
: Option.Option<Unify<A>>
Wraps the match result in an Option, representing an optional match.
When to use
Use to finalize a matcher when unmatched input is expected and should become
Option.none.
Details
This function ensures that the result of a matcher is wrapped in an Option,
making it easy to handle cases where no pattern matches. If a match is found,
it returns Some(value), otherwise, it returns None.
This is useful in cases where a missing match is expected and should be
handled explicitly rather than throwing an error or returning a default
value.
Example (Extracting a user role with Match.option)
import { Match } from "effect"
type User = { readonly role: "admin" | "editor" | "viewer" }
// Create a matcher to extract user roles
const getRole = Match.type<User>().pipe(
Match.when({ role: "admin" }, () => "Has full access"),
Match.when({ role: "editor" }, () => "Can edit content"),
Match.option // Wrap the result in an Option
)
console.log(getRole({ role: "admin" }))
// Output: { _id: 'Option', _tag: 'Some', value: 'Has full access' }
console.log(getRole({ role: "viewer" }))
// Output: { _id: 'Option', _tag: 'None' }
option: <function (type parameter) I in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>I, function (type parameter) F in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>F, function (type parameter) R in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>R, function (type parameter) A in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>A, function (type parameter) Pr in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>Pr, function (type parameter) Ret in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>Ret>(
self: Matcher<I, F, R, A, Pr, Ret>self: type Matcher<
Input,
Filters,
RemainingApplied,
Result,
Provided,
Return = any
> =
| TypeMatcher<
Input,
Filters,
RemainingApplied,
Result,
Return
>
| ValueMatcher<
Input,
Filters,
RemainingApplied,
Result,
Provided,
Return
>
Union type for matchers created by Match.type and Match.value.
Details
A Matcher carries the input type, accumulated filters, remaining cases,
result type, and, for value matchers, the provided value being matched.
Example (Matching string and number values)
import { Match } from "effect"
// Simulated dynamic input that can be a string or a number
const input: string | number = "some input"
// ┌─── string
// ▼
const result = Match.value(input).pipe(
// Match if the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match if the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are covered
Match.exhaustive
)
console.log(result)
// Output: "string: some input"
Matcher<function (type parameter) I in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>I, function (type parameter) F in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>F, function (type parameter) R in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>R, function (type parameter) A in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>A, function (type parameter) Pr in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>Pr, function (type parameter) Ret in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>Ret>
) => [function (type parameter) Pr in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>Pr] extends [never] ? (input: Iinput: function (type parameter) I in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>I) => 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<type Unify<A> = Values<ExtractTypes<FilterIn<A> & {
[typeSymbol]: A;
}>> extends infer Z ? Z | FilterInUnmatched<A, Keys<ExtractTypes<FilterIn<A> & {
[typeSymbol]: A;
}>>> | FilterOut<A> : never
Unifies types that implement the unification protocol.
When to use
Use to normalize unions of types that expose Effect's unification protocol.
Details
This type performs automatic type unification for types that contain
the unification symbols (unifySymbol, typeSymbol, ignoreSymbol).
It's primarily used internally by the Effect type system to handle
complex type unions and provide better type inference.
Example (Unifying protocol types)
import type { Unify } from "effect"
// Example of types that can be unified
type UnifiableA = {
value: string
[Unify.typeSymbol]?: string
[Unify.unifySymbol]?: { String: () => string }
}
type UnifiableB = {
value: number
[Unify.typeSymbol]?: number
[Unify.unifySymbol]?: { Number: () => number }
}
// Unify automatically handles the union
type Unified = Unify.Unify<UnifiableA | UnifiableB>
// Results in a properly unified type
Unify<function (type parameter) A in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<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<type Unify<A> = Values<ExtractTypes<FilterIn<A> & {
[typeSymbol]: A;
}>> extends infer Z ? Z | FilterInUnmatched<A, Keys<ExtractTypes<FilterIn<A> & {
[typeSymbol]: A;
}>>> | FilterOut<A> : never
Unifies types that implement the unification protocol.
When to use
Use to normalize unions of types that expose Effect's unification protocol.
Details
This type performs automatic type unification for types that contain
the unification symbols (unifySymbol, typeSymbol, ignoreSymbol).
It's primarily used internally by the Effect type system to handle
complex type unions and provide better type inference.
Example (Unifying protocol types)
import type { Unify } from "effect"
// Example of types that can be unified
type UnifiableA = {
value: string
[Unify.typeSymbol]?: string
[Unify.unifySymbol]?: { String: () => string }
}
type UnifiableB = {
value: number
[Unify.typeSymbol]?: number
[Unify.unifySymbol]?: { Number: () => number }
}
// Unify automatically handles the union
type Unified = Unify.Unify<UnifiableA | UnifiableB>
// Results in a properly unified type
Unify<function (type parameter) A in <I, F, R, A, Pr, Ret>(self: Matcher<I, F, R, A, Pr, Ret>): [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>> : Option.Option<Unify<A>>A>> = import internalinternal.const option: <I, F, R, A, Pr, Ret>(
self: Matcher<I, F, R, A, Pr, Ret>
) => [Pr] extends [never]
? (input: I) => Option.Option<Unify<A>>
: Option.Option<Unify<A>>
option