For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/api/functions/Result.let.md.
  • English
  • Function: let()

    Computes a plain value from the current success object and attaches it to the existing object under the specified key.

    • If the original result is a Failure, it is returned as-is.
    • If the original result is a Success, a new object is returned by shallow-merging: the original success object and { [name]: fn(value) }.

    This is the companion of bind for derivations that cannot fail. fn is expected to be a pure, synchronous computation. Use bind instead when the value comes from a fallible or asynchronous computation.

    let(name, fn)Select a case to follow its path

    Type Param

    N

    The key to assign the result of the fn computation.

    Type Param

    R1

    The input Result or ResultAsync.

    Type Param

    T2

    The value type returned by fn.

    Examples

    Success Case

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<{
        fullName: "Alice Smith";
        firstName: "Alice";
        lastName: "Smith";
    }, never>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }, never>, Result.Result<{
        fullName: "Alice Smith";
        firstName: "Alice";
        lastName: "Smith";
    }, never>>(a: Result.Result<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }, never>, ab: (a: Result.Result<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }, never>) => Result.Result<{
        fullName: "Alice Smith";
        firstName: "Alice";
        lastName: "Smith";
    }, never>): Result.Result<{
        fullName: "Alice Smith";
        firstName: "Alice";
        lastName: "Smith";
    }, never> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }>(value: {
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }): Result.Result<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    ({
    firstName: "Alice"
    firstName
    : 'Alice',
    lastName: "Smith"
    lastName
    : 'Smith' }),
    import Result
    Result
    .
    let<"fullName", Result.Result<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }, never>, "Alice Smith">(name: "fullName", fn: (a: {
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }) => "Alice Smith"): (result: Result.Result<{
        readonly firstName: "Alice";
        readonly lastName: "Smith";
    }, never>) => Result.Result<{
        fullName: "Alice Smith";
        firstName: "Alice";
        lastName: "Smith";
    }, never> (+1 overload)
    export let

    Computes a plain value from the current success object and attaches it to the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the original result is a Success , a new object is returned by shallow-merging: the original success object and { [name]: fn(value) }.

    This is the companion of bind for derivations that cannot fail. fn is expected to be a pure, synchronous computation. Use bind instead when the value comes from a fallible or asynchronous computation.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The value type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ firstName: 'Alice', lastName: 'Smith' }),
    Result.let('fullName', ({ firstName, lastName }) => `${firstName} ${lastName}`),
    );
    // { type: 'Success', value: { firstName: 'Alice', lastName: 'Smith', fullName: 'Alice Smith' } }
    @example

    Failure Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.let('fullName', ({ firstName, lastName }) => `${firstName} ${lastName}`),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Combined with bind

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.do(),
    Result.bind('x', () => Result.succeed(1)),
    Result.bind('y', () => Result.succeed(2)),
    Result.let('z', ({ x, y }) => x + y),
    );
    // { type: 'Success', value: { x: 1, y: 2, z: 3 } }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@seebind - Use this function instead when the value is computed by a Result -producing function.@categoryCombinators
    let
    ('fullName', ({
    firstName: "Alice"
    firstName
    ,
    lastName: "Smith"
    lastName
    }) => `${
    firstName: "Alice"
    firstName
    } ${
    lastName: "Smith"
    lastName
    }`),
    ); // { type: 'Success', value: { firstName: 'Alice', lastName: 'Smith', fullName: 'Alice Smith' } }

    Failure Case

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<{
        [x: string]: never;
        [x: number]: never;
        [x: symbol]: never;
    }, "error">
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<never, "error">, Result.Result<{
        [x: string]: never;
        [x: number]: never;
        [x: symbol]: never;
    }, "error">>(a: Result.Result<never, "error">, ab: (a: Result.Result<never, "error">) => Result.Result<{
        [x: string]: never;
        [x: number]: never;
        [x: symbol]: never;
    }, "error">): Result.Result<{
        [x: string]: never;
        [x: number]: never;
        [x: symbol]: never;
    }, "error"> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function fail<"error">(error: "error"): Result.Result<never, "error"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('error'),
    import Result
    Result
    .
    let<"fullName", Result.Result<never, "error">, never>(name: "fullName", fn: (a: never) => never): (result: Result.Result<never, "error">) => Result.Result<{
        [x: string]: never;
        [x: number]: never;
        [x: symbol]: never;
    }, "error"> (+1 overload)
    export let

    Computes a plain value from the current success object and attaches it to the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the original result is a Success , a new object is returned by shallow-merging: the original success object and { [name]: fn(value) }.

    This is the companion of bind for derivations that cannot fail. fn is expected to be a pure, synchronous computation. Use bind instead when the value comes from a fallible or asynchronous computation.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The value type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ firstName: 'Alice', lastName: 'Smith' }),
    Result.let('fullName', ({ firstName, lastName }) => `${firstName} ${lastName}`),
    );
    // { type: 'Success', value: { firstName: 'Alice', lastName: 'Smith', fullName: 'Alice Smith' } }
    @example

    Failure Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.let('fullName', ({ firstName, lastName }) => `${firstName} ${lastName}`),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Combined with bind

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.do(),
    Result.bind('x', () => Result.succeed(1)),
    Result.bind('y', () => Result.succeed(2)),
    Result.let('z', ({ x, y }) => x + y),
    );
    // { type: 'Success', value: { x: 1, y: 2, z: 3 } }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@seebind - Use this function instead when the value is computed by a Result -producing function.@categoryCombinators
    let
    ('fullName', ({
    firstName: never
    firstName
    ,
    lastName: never
    lastName
    }) => `${
    firstName: never
    firstName
    } ${
    lastName: never
    lastName
    }`),
    ); // { type: 'Failure', error: 'error' }

    Combined with bind

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<{
        x: 1;
        y: 2;
        z: number;
    }, never>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<{}, never>, Result.Result<{
        x: 1;
    }, never>, Result.Result<{
        x: 1;
        y: 2;
    }, never>, Result.Result<{
        x: 1;
        y: 2;
        z: number;
    }, never>>(a: Result.Result<{}, never>, ab: (a: Result.Result<{}, never>) => Result.Result<{
        x: 1;
    }, never>, bc: (b: Result.Result<{
        x: 1;
    }, never>) => Result.Result<{
        x: 1;
        y: 2;
    }, never>, cd: (c: Result.Result<{
        x: 1;
        y: 2;
    }, never>) => Result.Result<{
        x: 1;
        y: 2;
        z: number;
    }, never>): Result.Result<{
        x: 1;
        y: 2;
        z: number;
    }, never> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function do(): Result.Result<{}, never>
    export do

    Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.

    @example
    import { Result } from '@praha/byethrow';
    
    const result = Result.do();
    // Result.Result<{}, never>
    @categoryCreators
    do
    (),
    import Result
    Result
    .
    function bind<"x", Result.Result<{}, never>, Result.Result<1, never>>(name: "x", fn: (a: {}) => Result.Result<1, never>): (result: Result.Result<{}, never>) => Result.Result<{
        x: 1;
    }, never> (+1 overload)

    Chains another Result -producing computation and merges its success value into the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the next result is a Failure , it is returned as-is.
    • If both are Success , a new object is returned by shallow-merging: the original success object and { [name]: nextSuccess }.

    This is useful for building up objects in a compositional and type-safe way, especially in validation or data-fetching pipelines.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Success', value: { name: 1, age: 20 } }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.fail('error')),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    bind
    ('x', () =>
    import Result
    Result
    .
    function succeed<1>(value: 1): Result.Result<1, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (1)),
    import Result
    Result
    .
    function bind<"y", Result.Result<{
        x: 1;
    }, never>, Result.Result<2, never>>(name: "y", fn: (a: {
        x: 1;
    }) => Result.Result<2, never>): (result: Result.Result<{
        x: 1;
    }, never>) => Result.Result<{
        x: 1;
        y: 2;
    }, never> (+1 overload)

    Chains another Result -producing computation and merges its success value into the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the next result is a Failure , it is returned as-is.
    • If both are Success , a new object is returned by shallow-merging: the original success object and { [name]: nextSuccess }.

    This is useful for building up objects in a compositional and type-safe way, especially in validation or data-fetching pipelines.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Success', value: { name: 1, age: 20 } }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.fail('error')),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    bind
    ('y', () =>
    import Result
    Result
    .
    function succeed<2>(value: 2): Result.Result<2, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (2)),
    import Result
    Result
    .
    let<"z", Result.Result<{
        x: 1;
        y: 2;
    }, never>, number>(name: "z", fn: (a: {
        x: 1;
        y: 2;
    }) => number): (result: Result.Result<{
        x: 1;
        y: 2;
    }, never>) => Result.Result<{
        x: 1;
        y: 2;
        z: number;
    }, never> (+1 overload)
    export let

    Computes a plain value from the current success object and attaches it to the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the original result is a Success , a new object is returned by shallow-merging: the original success object and { [name]: fn(value) }.

    This is the companion of bind for derivations that cannot fail. fn is expected to be a pure, synchronous computation. Use bind instead when the value comes from a fallible or asynchronous computation.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The value type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ firstName: 'Alice', lastName: 'Smith' }),
    Result.let('fullName', ({ firstName, lastName }) => `${firstName} ${lastName}`),
    );
    // { type: 'Success', value: { firstName: 'Alice', lastName: 'Smith', fullName: 'Alice Smith' } }
    @example

    Failure Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.let('fullName', ({ firstName, lastName }) => `${firstName} ${lastName}`),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Combined with bind

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.do(),
    Result.bind('x', () => Result.succeed(1)),
    Result.bind('y', () => Result.succeed(2)),
    Result.let('z', ({ x, y }) => x + y),
    );
    // { type: 'Success', value: { x: 1, y: 2, z: 3 } }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@seebind - Use this function instead when the value is computed by a Result -producing function.@categoryCombinators
    let
    ('z', ({
    x: 1
    x
    ,
    y: 2
    y
    }) =>
    x: 1
    x
    +
    y: 2
    y
    ),
    ); // { type: 'Success', value: { x: 1, y: 2, z: 3 } }

    See

    • pipe - It is recommended to use this function with the pipe function for better readability and composability.
    • bind - Use this function instead when the value is computed by a Result-producing function.

    Call Signature

    let<N, R1, T2>(name, fn): (result) => InferSuccess<R1> extends object ? ResultFor<R1, { [K in string | number | symbol]: K extends Exclude<keyof InferSuccess, N> ? InferSuccess[K] : T2 }, InferFailure<R1>> : unknown

    Type Parameters

    N

    N extends string

    R1

    R1 extends ResultMaybeAsync<any, any>

    T2

    T2

    Parameters

    name

    N

    fn

    (a) => T2

    Returns

    (result) => InferSuccess<R1> extends object ? ResultFor<R1, { [K in string | number | symbol]: K extends Exclude<keyof InferSuccess, N> ? InferSuccess[K] : T2 }, InferFailure<R1>> : unknown

    Call Signature

    let<N, T1, T2>(name, fn): <R1>(result) => T1 extends object ? ResultFor<R1, { [K in string | number | symbol]: K extends Exclude<keyof T1, N> ? T1[K] : T2 }, InferFailure<R1>> : unknown

    Type Parameters

    N

    N extends string

    T1

    T1

    T2

    T2

    Parameters

    name

    N

    fn

    (a) => T2

    Returns

    <R1>(result) => T1 extends object ? ResultFor<R1, { [K in string | number | symbol]: K extends Exclude<keyof T1, N> ? T1[K] : T2 }, InferFailure<R1>> : unknown