> For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt.

# Namespace: Result

Re-exports core Result-handling utilities under two convenient namespaces:

- `Result`: Verbose and explicit usage
- `R`: Shorthand alias for more concise code

## Examples

**Basic Usage**

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

const validateId = (id: string) => {
  if (!id.startsWith('u')) {
    return Result.fail(new Error('Invalid ID format'));
  }
  return Result.succeed();
};

const findUser = Result.fn({
  try: (id: string) => {
    return { id, name: 'John Doe' };
  },
  catch: (error) => new Error('Failed to find user', { cause: error }),
});

const result = Result.pipe(
  Result.succeed('u123'),
  Result.andThrough(validateId),
  Result.andThen(findUser),
);

if (Result.isSuccess(result)) {
  console.log(result.value); // User found: John Doe
}
```

**Shorthand Usage**

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

const validateId = (id: string) => {
  if (!id.startsWith('u')) {
    return R.fail(new Error('Invalid ID format'));
  }
  return R.succeed();
};

const findUser = R.fn({
  try: (id: string) => {
    return { id, name: 'John Doe' };
  },
  catch: (error) => new Error('Failed to find user', { cause: error }),
});

const result = R.pipe(
  R.succeed('u123'),
  R.andThrough(validateId),
  R.andThen(findUser),
);

if (R.isSuccess(result)) {
  console.log(result.value); // User found: John Doe
}
```

## Core Types

| Type Alias                                                         | Description                                                                                        |
| ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- |
| [Failure](/byethrow/api/types/Result.Failure.md)                   | Represents a failed result.                                                                        |
| [Result](/byethrow/api/types/Result.Result.md)                     | A union type representing either a success or a failure.                                           |
| [ResultAsync](/byethrow/api/types/Result.ResultAsync.md)           | An asynchronous variant of [Result](/byethrow/api/types/Result.Result.md), wrapped in a `Promise`. |
| [ResultFor](/byethrow/api/types/Result.ResultFor.md)               | Resolves to the appropriate Result type (sync or async) based on the input type.                   |
| [ResultMaybeAsync](/byethrow/api/types/Result.ResultMaybeAsync.md) | A result that may be either synchronous or asynchronous.                                           |
| [Success](/byethrow/api/types/Result.Success.md)                   | Represents a successful result.                                                                    |

## Infer Types

| Type Alias                                                 | Description                                                                                                                |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| [InferFailure](/byethrow/api/types/Result.InferFailure.md) | Infers the [Failure](/byethrow/api/types/Result.Failure.md) value type `E` from a Result or a function returning a Result. |
| [InferSuccess](/byethrow/api/types/Result.InferSuccess.md) | Infers the [Success](/byethrow/api/types/Result.Success.md) value type `T` from a Result or a function returning a Result. |

## Creators

| Function                                             | Description                                                                                                                                                                           |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [do](/byethrow/api/functions/Result.do.md)           | Alias for `succeed({})`. Commonly used as a neutral base value in functional chains or monadic pipelines.                                                                             |
| [fail](/byethrow/api/functions/Result.fail.md)       | Creates a [Failure](/byethrow/api/types/Result.Failure.md) result from a given error.                                                                                                 |
| [fn](/byethrow/api/functions/Result.fn.md)           | Wraps a function that may throw and returns a new function that returns a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md). |
| [succeed](/byethrow/api/functions/Result.succeed.md) | Creates a [Success](/byethrow/api/types/Result.Success.md) result from a given value.                                                                                                 |
| [try](/byethrow/api/functions/Result.try.md)         | Executes a function that may throw and wraps the result in a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md).              |

## Combinators

| Function                                                       | Description                                                                                                                                                                                                                                                                                                                                                |
| -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [andThen](/byethrow/api/functions/Result.andThen.md)           | Chains the next computation using the success value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md). If the original result is a [Failure](/byethrow/api/types/Result.Failure.md), it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is. |
| [andThrough](/byethrow/api/functions/Result.andThrough.md)     | Runs an additional computation using the success value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md), but **returns the original result** if the additional computation is successful.                                                                                                   |
| [bind](/byethrow/api/functions/Result.bind.md)                 | Chains another [Result](/byethrow/api/types/Result.Result.md)-producing computation and **merges its success value** into the existing object under the specified key.                                                                                                                                                                                     |
| [inspect](/byethrow/api/functions/Result.inspect.md)           | Executes a side effect function on the success value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md), without modifying the original result. This is useful for debugging, logging, or performing other side effects while maintaining the original value and error state.                 |
| [inspectError](/byethrow/api/functions/Result.inspectError.md) | Executes a side effect function on the error value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md), without modifying the original result. This is useful for debugging, logging, or performing other side effects while maintaining the original value and error state.                   |
| [map](/byethrow/api/functions/Result.map.md)                   | Applies a transformation function to the success value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md). If the input is a [Failure](/byethrow/api/types/Result.Failure.md), it will be returned unchanged.                                                                                 |
| [mapError](/byethrow/api/functions/Result.mapError.md)         | Applies a transformation function to the error value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md). If the input is a [Success](/byethrow/api/types/Result.Success.md), it will be returned unchanged.                                                                                   |
| [orElse](/byethrow/api/functions/Result.orElse.md)             | Chains the next computation using the error value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md). If the original result is a [Success](/byethrow/api/types/Result.Success.md), it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.   |
| [orThrough](/byethrow/api/functions/Result.orThrough.md)       | Runs an additional computation using the error value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md), but **returns the original failure** if the additional computation is successful.                                                                                                    |

## Unwraps

| Function                                                     | Description                                                                                                                                   |
| ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| [unwrap](/byethrow/api/functions/Result.unwrap.md)           | Extracts the success value from a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md). |
| [unwrapError](/byethrow/api/functions/Result.unwrapError.md) | Extracts the error value from a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md).   |

## Assertions

| Function                                                         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [assertFailure](/byethrow/api/functions/Result.assertFailure.md) | Asserts that a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md) is a [Failure](/byethrow/api/types/Result.Failure.md) and returns it. This function requires that the result's success type is `never`, meaning the result is guaranteed to be a [Failure](/byethrow/api/types/Result.Failure.md) at the type level. If the result is a [Success](/byethrow/api/types/Result.Success.md) at runtime, throws an error. |
| [assertSuccess](/byethrow/api/functions/Result.assertSuccess.md) | Asserts that a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md) is a [Success](/byethrow/api/types/Result.Success.md) and returns it. This function requires that the result's error type is `never`, meaning the result is guaranteed to be a [Success](/byethrow/api/types/Result.Success.md) at the type level. If the result is a [Failure](/byethrow/api/types/Result.Failure.md) at runtime, throws an error.   |

## Type Guards

| Function                                                 | Description                                                                                                                    |
| -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| [isFailure](/byethrow/api/functions/Result.isFailure.md) | Type guard to check if a [Result](/byethrow/api/types/Result.Result.md) is a [Failure](/byethrow/api/types/Result.Failure.md). |
| [isResult](/byethrow/api/functions/Result.isResult.md)   | Type guard to check if a value is a [Result](/byethrow/api/types/Result.Result.md).                                            |
| [isSuccess](/byethrow/api/functions/Result.isSuccess.md) | Type guard to check if a [Result](/byethrow/api/types/Result.Result.md) is a [Success](/byethrow/api/types/Result.Success.md). |

## Utilities

| Function                                               | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [collect](/byethrow/api/functions/Result.collect.md)   | Processes multiple [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md) values into a single result. If all results are [Success](/byethrow/api/types/Result.Success.md), returns a [Success](/byethrow/api/types/Result.Success.md) containing all values. If any result is a [Failure](/byethrow/api/types/Result.Failure.md), returns a [Failure](/byethrow/api/types/Result.Failure.md) containing an array of all errors.                       |
| [parse](/byethrow/api/functions/Result.parse.md)       | Parses a value using a [Standard Schema](https://github.com/standard-schema/standard-schema) compatible schema. Returns a [Result](/byethrow/api/types/Result.Result.md) with the parsed value on success or validation errors on failure.                                                                                                                                                                                                                                                                 |
| [pipe](/byethrow/api/functions/Result.pipe.md)         | Applies a sequence of functions to a value, from left to right.                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| [sequence](/byethrow/api/functions/Result.sequence.md) | Processes multiple [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md) values into a single result. If all results are [Success](/byethrow/api/types/Result.Success.md), returns a [Success](/byethrow/api/types/Result.Success.md) containing all values. If any result is a [Failure](/byethrow/api/types/Result.Failure.md), immediately stops processing and returns a [Failure](/byethrow/api/types/Result.Failure.md) with that single error. |
