Result を検証する

orElseandThen などの操作ですべてのエラーを処理し尽くした場合、assertSuccessassertFailure を使用してコンパイル時に Result 型を検証できます。 unwrapunwrapError と組み合わせることで、安全な値の取り出しが可能になります。

assertSuccess で成功をアサートする

assertSuccess 関数は ResultSuccess であることを検証します。 この関数はエラー型が never であることを要求し、コンパイル時に Result が確実に Success であることを保証します。

import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<number, never>
result
:
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, never> =
import Result
Result
.
const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
succeed
(42);
const
const success: Result.Success<number>
success
=
import Result
Result
.
const assertSuccess: <Result.Result<number, never>>(result: Result.Result<number, never>) => Result.Success<number>

Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@example

Type-safe usage after narrowing error type

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

const getResult = (): Result.Result<number, string> => Result.succeed(42);
const value = Result.pipe(
getResult(),
Result.orElse(() => Result.succeed(0)), // Error type becomes never
Result.assertSuccess, // Type-safe: error type is now never
Result.unwrap(), // Safe unwrap after assertion
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
(
const result: Result.Result<number, never>
result
);
// success: { type: 'Success', value: 42 }

エラー型が never でない場合

エラー型が never でない場合、TypeScript はコンパイルエラーを出します。

import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<number, string>
result
:
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, string> =
import Result
Result
.
const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
succeed
(42);
// ❌ TypeScript エラー: エラー型が string であり never ではない const
const success: Promise<Result.Success<any>>
success
=
import Result
Result
.
const assertSuccess: <Result.ResultMaybeAsync<any, never>>(result: Result.ResultMaybeAsync<any, never>) => Promise<Result.Success<any>>

Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@example

Type-safe usage after narrowing error type

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

const getResult = (): Result.Result<number, string> => Result.succeed(42);
const value = Result.pipe(
getResult(),
Result.orElse(() => Result.succeed(0)), // Error type becomes never
Result.assertSuccess, // Type-safe: error type is now never
Result.unwrap(), // Safe unwrap after assertion
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
(result);
Argument of type 'Result<number, string>' is not assignable to parameter of type 'ResultMaybeAsync<any, never>'. Type 'Failure<string>' is not assignable to type 'ResultMaybeAsync<any, never>'. Type 'Failure<string>' is not assignable to type 'Failure<never> | Promise<Result<any, never>>'. Type 'Failure<string>' is not assignable to type 'Failure<never>'. Type 'string' is not assignable to type 'never'.

orElse でエラー型を never にする

orElse を使用してすべてのエラーを処理し、エラー型を never に絞り込みます。

import { 
import Result
Result
} from '@praha/byethrow';
declare const
const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
getResult
: () =>
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, 'NotFound' | 'NetworkError'>;
const
const result: Result.Success<number>
result
=
import Result
Result
.
const pipe: <Result.Result<number, "NotFound" | "NetworkError">, Result.Result<number, never>, Result.Success<number>>(a: Result.Result<number, "NotFound" | "NetworkError">, ab: (a: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => Result.Success<number>) => Result.Success<number> (+25 overloads)
pipe
(
const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
getResult
(),
import Result
Result
.
const orElse: <Result.Result<number, "NotFound" | "NetworkError">, Result.Result<0, never>>(fn: (a: "NotFound" | "NetworkError") => Result.Result<0, never>) => (result: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never> (+1 overload)
orElse
((
error: "NotFound" | "NetworkError"
error
) => {
// すべてのエラーを処理してフォールバック値を返す return
import Result
Result
.
const succeed: <0>(value: 0) => Result.Result<0, never> (+1 overload)
succeed
(0);
}), // エラー型は never になった
import Result
Result
.
const assertSuccess: <R extends Result.ResultMaybeAsync<any, never>>(result: R) => true extends HasPromise<R> ? Promise<Result.Success<Result.InferSuccess<R>>> : Result.Success<Result.InferSuccess<R>>

Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@example

Type-safe usage after narrowing error type

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

const getResult = (): Result.Result<number, string> => Result.succeed(42);
const value = Result.pipe(
getResult(),
Result.orElse(() => Result.succeed(0)), // Error type becomes never
Result.assertSuccess, // Type-safe: error type is now never
Result.unwrap(), // Safe unwrap after assertion
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
,
); // result: { type: 'Success', value: number }

assertFailure で失敗を検証する

assertFailure 関数は逆の動作で ResultFailure であることを検証します。 この関数は成功型が never であることを要求します。

import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<never, string>
result
:
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<never, string> =
import Result
Result
.
const fail: <"error">(error: "error") => Result.Result<never, "error"> (+1 overload)
fail
('error');
const
const failure: Result.Failure<string>
failure
=
import Result
Result
.
const assertFailure: <Result.Result<never, string>>(result: Result.Result<never, string>) => Result.Failure<string>

Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.fail('error');
const failure = Result.assertFailure(result);
// failure: { type: 'Failure', error: 'error' }
@example

Type-safe usage after narrowing success type

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

const getResult = (): Result.Result<number, string> => Result.fail('error');
const value = Result.pipe(
getResult(),
Result.andThen(() => Result.fail('die')), // Success type becomes never
Result.assertFailure, // Type-safe: success type is now never
Result.unwrapError(), // Safe unwrap after assertion
);
@seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
assertFailure
(
const result: Result.Result<never, string>
result
);
// failure: { type: 'Failure', error: 'error' }

成功型が never でない場合

成功型が never でない場合、TypeScript はコンパイルエラーを出します。

import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<number, string>
result
:
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, string> =
import Result
Result
.
const fail: <"error">(error: "error") => Result.Result<never, "error"> (+1 overload)
fail
('error');
// ❌ TypeScript エラー: 成功型が number であり never ではない const
const failure: Promise<Result.Failure<any>>
failure
=
import Result
Result
.
const assertFailure: <Result.ResultMaybeAsync<never, any>>(result: Result.ResultMaybeAsync<never, any>) => Promise<Result.Failure<any>>

Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.fail('error');
const failure = Result.assertFailure(result);
// failure: { type: 'Failure', error: 'error' }
@example

Type-safe usage after narrowing success type

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

const getResult = (): Result.Result<number, string> => Result.fail('error');
const value = Result.pipe(
getResult(),
Result.andThen(() => Result.fail('die')), // Success type becomes never
Result.assertFailure, // Type-safe: success type is now never
Result.unwrapError(), // Safe unwrap after assertion
);
@seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
assertFailure
(result);
Argument of type 'Result<number, string>' is not assignable to parameter of type 'ResultMaybeAsync<never, any>'. Type 'Success<number>' is not assignable to type 'ResultMaybeAsync<never, any>'. Type 'Success<number>' is not assignable to type 'Success<never> | Promise<Result<never, any>>'. Type 'Success<number>' is not assignable to type 'Success<never>'. Type 'number' is not assignable to type 'never'.

andThen で成功型を never にする

andThen を使用してすべてのパスを失敗にし、成功型を never に絞り込みます。

import { 
import Result
Result
} from '@praha/byethrow';
declare const
const getResult: () => Result.Result<number, string>
getResult
: () =>
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, string>;
const
const result: Result.Failure<string>
result
=
import Result
Result
.
const pipe: <Result.Result<number, string>, Result.Result<never, string>, Result.Failure<string>>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<never, string>, bc: (b: Result.Result<never, string>) => Result.Failure<string>) => Result.Failure<string> (+25 overloads)
pipe
(
const getResult: () => Result.Result<number, string>
getResult
(),
import Result
Result
.
const andThen: <Result.Result<number, string>, Result.Result<never, "converted error">>(fn: (a: number) => Result.Result<never, "converted error">) => (result: Result.Result<number, string>) => Result.Result<never, string> (+1 overload)
andThen
(() => {
// すべてのパスが失敗に至る return
import Result
Result
.
const fail: <"converted error">(error: "converted error") => Result.Result<never, "converted error"> (+1 overload)
fail
('converted error');
}), // 成功型は never になった
import Result
Result
.
const assertFailure: <R extends Result.ResultMaybeAsync<never, any>>(result: R) => true extends HasPromise<R> ? Promise<Result.Failure<Result.InferFailure<R>>> : Result.Failure<Result.InferFailure<R>>

Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.fail('error');
const failure = Result.assertFailure(result);
// failure: { type: 'Failure', error: 'error' }
@example

Type-safe usage after narrowing success type

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

const getResult = (): Result.Result<number, string> => Result.fail('error');
const value = Result.pipe(
getResult(),
Result.andThen(() => Result.fail('die')), // Success type becomes never
Result.assertFailure, // Type-safe: success type is now never
Result.unwrapError(), // Safe unwrap after assertion
);
@seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
assertFailure
,
); // result: { type: 'Failure', error: string }

assertSuccess + unwrap で安全にアンラップする

assertSuccessunwrap を組み合わせることで、Result から安全に値を取り出せます。 検証後はエラー型が never になるため、unwrap がエラーを投げないことが保証されます。

import { 
import Result
Result
} from '@praha/byethrow';
declare const
const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
getResult
: () =>
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, 'NotFound' | 'NetworkError'>;
const
const value: number
value
=
import Result
Result
.
const pipe: <Result.Result<number, "NotFound" | "NetworkError">, Result.Result<number, never>, Result.Success<number>, number>(a: Result.Result<number, "NotFound" | "NetworkError">, ab: (a: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => Result.Success<number>, cd: (c: Result.Success<number>) => number) => number (+25 overloads)
pipe
(
const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
getResult
(),
import Result
Result
.
const orElse: <Result.Result<number, "NotFound" | "NetworkError">, Result.Result<0, never>>(fn: (a: "NotFound" | "NetworkError") => Result.Result<0, never>) => (result: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never> (+1 overload)
orElse
(() =>
import Result
Result
.
const succeed: <0>(value: 0) => Result.Result<0, never> (+1 overload)
succeed
(0)), // すべてのエラーを処理
import Result
Result
.
const assertSuccess: <R extends Result.ResultMaybeAsync<any, never>>(result: R) => true extends HasPromise<R> ? Promise<Result.Success<Result.InferSuccess<R>>> : Result.Success<Result.InferSuccess<R>>

Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@example

Type-safe usage after narrowing error type

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

const getResult = (): Result.Result<number, string> => Result.succeed(42);
const value = Result.pipe(
getResult(),
Result.orElse(() => Result.succeed(0)), // Error type becomes never
Result.assertSuccess, // Type-safe: error type is now never
Result.unwrap(), // Safe unwrap after assertion
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
, // エラー型は never になった
import Result
Result
.
const unwrap: <Result.Success<number>>() => (result: Result.Success<number>) => number (+3 overloads)
unwrap
(), // 安全: エラーを投げないことが保証されている
); // value: number

このパターンは、型安全性を確保しつつ最終的な値を取り出したいアプリケーションの境界で特に有用です。

ランタイム型ガードとの違い

assertSuccess/assertFailureisSuccess/isFailure の違いを理解することが重要です。

関数目的不一致時にエラーを投げるか
isSuccessランタイムチェック、boolean を返すいいえ
isFailureランタイムチェック、boolean を返すいいえ
assertSuccessコンパイル時検証(never を要求)はい(型が間違っている場合)
assertFailureコンパイル時検証(never を要求)はい(型が間違っている場合)
  • isSuccess/isFailure: ランタイムで Result 型に基づいて分岐する必要がある場合に使用
  • assertSuccess/assertFailure: コンパイル時に Result 型が確定している場合に検証するために使用

リファレンス

関数目的
assertSuccess(result)Result が Success であることを検証する(エラー型が never である必要あり)
assertFailure(result)Result が Failure であることを検証する(成功型が never である必要あり)