#Result を作成する
Result 型を理解したところで、ライブラリのヘルパー関数を使って Result を作成する方法を学びましょう。
#succeed で成功を表す Result を作成する
succeed 関数は成功を表す Result を作成します。
import { import Result Result } from '@praha/byethrow';
// 値を持つ場合
const const success: Result.Result<42, never> success = import Result Result .const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload) succeed (42);
// 型: Result.Result<number, never>
// 値を持たない場合(void)
const const voidSuccess: Result.Result<void, never> voidSuccess = import Result Result .const succeed: () => Result.ResultFor<never, void, never> (+1 overload) succeed ();
// 型: Result.Result<void, never>#非同期値の場合
Promise を渡すと、succeed は自動的に ResultAsync を返します。
import { import Result Result } from '@praha/byethrow';
const const asyncSuccess: Result.ResultAsync<string, never> asyncSuccess = import Result Result .const succeed: <Promise<string>>(value: Promise<string>) => Result.ResultAsync<string, never> (+1 overload) succeed (var Promise: PromiseConstructorRepresents the completion of an asynchronous operation
Promise .PromiseConstructor.resolve<string>(value: string): Promise<string> (+2 overloads)Creates a new resolved promise for the provided value.
@paramvalue A promise.@returnsA promise whose internal state matches the provided promise. resolve ('hello'));
// 型: Result.ResultAsync<string, never>
const const resolved: Result.Result<string, never> resolved = await const asyncSuccess: Result.ResultAsync<string, never> asyncSuccess ;
// 型: Result.Result<string, never>#fail で失敗を表す Result を作成する
fail 関数は失敗を表す Result を作成します。
import { import Result Result } from '@praha/byethrow';
// エラー値を持つ場合
const const failure: Result.Result<never, "Something went wrong"> failure = import Result Result .const fail: <"Something went wrong">(error: "Something went wrong") => Result.Result<never, "Something went wrong"> (+1 overload) fail ('Something went wrong');
// 型: Result.Result<never, string>
// 値を持たない場合(void)
const const voidFailure: Result.Result<never, void> voidFailure = import Result Result .const fail: () => Result.ResultFor<never, never, void> (+1 overload) fail ();
// 型: Result.Result<never, void>#非同期エラーの場合
succeed と同様に、Promise を渡すと fail は ResultAsync を返します。
import { import Result Result } from '@praha/byethrow';
const const asyncFailure: Result.ResultAsync<never, string> asyncFailure = import Result Result .const fail: <Promise<string>>(error: Promise<string>) => Result.ResultAsync<never, string> (+1 overload) fail (var Promise: PromiseConstructorRepresents the completion of an asynchronous operation
Promise .PromiseConstructor.resolve<string>(value: string): Promise<string> (+2 overloads)Creates a new resolved promise for the provided value.
@paramvalue A promise.@returnsA promise whose internal state matches the provided promise. resolve ('async error'));
// 型: Result.ResultAsync<never, string>
const const resolved: Result.Result<never, string> resolved = await const asyncFailure: Result.ResultAsync<never, string> asyncFailure ;
// 型: Result.Result<never, string>#do でパイプラインを開始する
do 関数は空のオブジェクトでパイプラインを開始する便利な方法です。
import { import Result Result } from '@praha/byethrow';
const const start: Result.Result<{}, never> start = import Result Result .function do(): import("/home/runner/work/byethrow/byethrow/packages/byethrow/dist/esm/result").Result<{}, never>
export do
Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.
@function@exampleimport { Result } from '@praha/byethrow';
const result = Result.do();
// Result.Result<{}, never>
@categoryCreators do ();
// 型: Result.Result<{}, never>これは、オブジェクトを段階的に構築する際に特に便利です(このパターンは後のセクションで詳しく解説します)。
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<{
name: "Alice";
age: 30;
}, never>
result = import Result Result .const pipe: <Result.Result<{}, never>, Result.Result<{
name: "Alice";
}, never>, Result.Result<{
name: "Alice";
age: 30;
}, never>>(a: Result.Result<{}, never>, ab: (a: Result.Result<{}, never>) => Result.Result<{
name: "Alice";
}, never>, bc: (b: Result.Result<{
name: "Alice";
}, never>) => Result.Result<{
name: "Alice";
age: 30;
}, never>) => Result.Result<{
name: "Alice";
age: 30;
}, never> (+25 overloads)
pipe (
import Result Result .function do(): import("/home/runner/work/byethrow/byethrow/packages/byethrow/dist/esm/result").Result<{}, never>
export do
Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.
@function@exampleimport { Result } from '@praha/byethrow';
const result = Result.do();
// Result.Result<{}, never>
@categoryCreators do (),
import Result Result .const bind: <"name", Result.Result<{}, never>, Result.Result<"Alice", never>>(name: "name", fn: (a: {}) => Result.Result<"Alice", never>) => (result: Result.Result<{}, never>) => Result.Result<{
name: "Alice";
}, never> (+1 overload)
bind ('name', () => import Result Result .const succeed: <"Alice">(value: "Alice") => Result.Result<"Alice", never> (+1 overload) succeed ('Alice')),
import Result Result .const bind: <"age", Result.Result<{
name: "Alice";
}, never>, Result.Result<30, never>>(name: "age", fn: (a: {
name: "Alice";
}) => Result.Result<30, never>) => (result: Result.Result<{
name: "Alice";
}, never>) => Result.Result<{
name: "Alice";
age: 30;
}, never> (+1 overload)
bind ('age', () => import Result Result .const succeed: <30>(value: 30) => Result.Result<30, never> (+1 overload) succeed (30)),
);
// 型: Result.Result<{ name: string; age: number }, never>#リファレンス
| 関数 | 目的 |
|---|---|
| succeed(value) | Success の結果を作成 |
| fail(error) | Failure の結果を作成 |
| do() | 空のオブジェクトで開始 |
