type-alias / @typed/guard

Guard

An effectful partial transformation.

A successful Some contains a match, a successful None means the input did not match, and an Effect failure remains in the E channel. Required services remain in R.

Package version
1.0.0-beta.13
Category
Guard contracts
Since
1.0.0

Import

import { Guard } from "@typed/guard";

Signatures

export type Guard<in I, out O, out E = never, out R = never> = (input: I) => Effect.Effect<Option.Option<O>, E, R>;
export declare namespace Guard {
    type Input<T> = [
        T
    ] extends [
        Guard<infer I, infer _R, infer _E, infer _O>
    ] ? I : [
        T
    ] extends [
        AsGuard<infer I, infer _R, infer _E, infer _O>
    ] ? I : never;
    type Services<T> = [
        T
    ] extends [
        Guard<infer _I, infer _O, infer _E, infer R>
    ] ? R : [
        T
    ] extends [
        AsGuard<infer _I, infer _O, infer _E, infer R>
    ] ? R : never;
    type Error<T> = [
        T
    ] extends [
        Guard<infer _I, infer _O, infer E, infer _R>
    ] ? E : [
        T
    ] extends [
        AsGuard<infer _I, infer _O, infer E, infer _R>
    ] ? E : never;
    type Output<T> = [
        T
    ] extends [
        Guard<infer _I, infer O, infer _E, infer _R>
    ] ? O : [
        T
    ] extends [
        AsGuard<infer _I, infer O, infer _E, infer _R>
    ] ? O : never;
}

Why

Guard models non-match as successful None, keeping ordinary dispatch separate from typed failure, defects, and interruption in Effect’s Cause.

Ownership and lifetime

A Guard acquires no resources by itself; each invocation has the lifetime and service requirements of the returned Effect.

See Effect Option and Effect error management.

Examples

import type { Guard } from "@typed/guard"
import { Effect, Option } from "effect"
const string: Guard<unknown, string> = (input) => Effect.succeed(typeof input === "string" ? Option.some(input) : Option.none())

Members

  • Guard.Error

    Extracts the typed error channel from a Guard or Guard adapter.

  • Guard.Input

    Extracts the accepted input type from a Guard or Guard adapter.

  • Guard.Output

    Extracts the matched output type from a Guard or Guard adapter.

  • Guard.Services

    Extracts the Effect service requirements from a Guard or Guard adapter.

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source