# Function: bind()

Chains another [Result](../types/Result.Result.md)-producing computation and **merges its success value**
into the existing object under the specified key.

- If the original result is a [Failure](../types/Result.Failure.md), it is returned as-is.
- If the next result is a [Failure](../types/Result.Failure.md), it is returned as-is.
- If both are [Success](../types/Result.Success.md), 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.

## Type Param

**N**

The key to assign the result of the `fn` computation.

## Type Param

**R1**

The input [Result](../types/Result.Result.md) or [ResultAsync](../types/Result.ResultAsync.md).

## Type Param

**R2**

The result type returned by `fn`.

## Examples

**Success Case**

```ts
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 } }
```

**Failure Case (input is a Failure)**

```ts
import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.fail('error'),
  Result.bind('age', (user) => Result.succeed(20)),
);
// { type: 'Failure', error: 'error' }
```

**Failure Case (function returns a Failure)**

```ts
import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.succeed({ name: 'Alice' }),
  Result.bind('age', (user) => Result.fail('error')),
);
// { type: 'Failure', error: 'error' }
```

## See

[pipe](./Result.pipe.md) - It is recommended to use this function with the [pipe](./Result.pipe.md) function for better readability and composability.

## Call Signature

> **bind**\<`N`, `R1`, `R2`>(`name`, `fn`): (`result`) => [`InferSuccess`](../types/Result.InferSuccess.md)\<`R1`> _extends_ `object` ? [`ResultFor`](../types/Result.ResultFor.md)\<`R1` | `R2`, \{ \[K in string | number | symbol]: K extends Exclude\<keyof InferSuccess, N> ? InferSuccess\[K] : InferSuccess\<R2> }, [`InferFailure`](../types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](../types/Result.InferFailure.md)\<`R2`>> : `unknown`

### Type Parameters

#### N

`N` _extends_ `string`

#### R1

`R1` _extends_ [`ResultMaybeAsync`](../types/Result.ResultMaybeAsync.md)\<`any`, `any`>

#### R2

`R2` _extends_ [`ResultMaybeAsync`](../types/Result.ResultMaybeAsync.md)\<`any`, `any`>

### Parameters

#### name

`N`

#### fn

(`a`) => `R2`

### Returns

(`result`) => [`InferSuccess`](../types/Result.InferSuccess.md)\<`R1`> _extends_ `object` ? [`ResultFor`](../types/Result.ResultFor.md)\<`R1` | `R2`, \{ \[K in string | number | symbol]: K extends Exclude\<keyof InferSuccess, N> ? InferSuccess\[K] : InferSuccess\<R2> }, [`InferFailure`](../types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](../types/Result.InferFailure.md)\<`R2`>> : `unknown`

## Call Signature

> **bind**\<`N`, `F`>(`name`, `fn`): \<`R1`>(`result`) => `Parameters`\<`F`>\[`0`] _extends_ `object` ? [`ResultFor`](../types/Result.ResultFor.md)\<`R1` | `ReturnType`\<`F`>, \{ \[K in string | number | symbol]: K extends Exclude\<keyof any\[any], N> ? any\[any]\[K] : InferSuccess\<F> }, [`InferFailure`](../types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](../types/Result.InferFailure.md)\<`F`>> : `unknown`

### Type Parameters

#### N

`N` _extends_ `string`

#### F

`F` _extends_ (`a`) => [`ResultMaybeAsync`](../types/Result.ResultMaybeAsync.md)\<`any`, `any`>

### Parameters

#### name

`N`

#### fn

`F`

### Returns

\<`R1`>(`result`) => `Parameters`\<`F`>\[`0`] _extends_ `object` ? [`ResultFor`](../types/Result.ResultFor.md)\<`R1` | `ReturnType`\<`F`>, \{ \[K in string | number | symbol]: K extends Exclude\<keyof any\[any], N> ? any\[any]\[K] : InferSuccess\<F> }, [`InferFailure`](../types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](../types/Result.InferFailure.md)\<`F`>> : `unknown`
