#prefer-result-maybe-async
Result<T, E> | ResultAsync<T, E> というユニオン型の代わりに ResultMaybeAsync<T, E> の使用を強制します。
#ルールの詳細
ResultMaybeAsync<T, E> は Result<T, E> | ResultAsync<T, E> のエイリアスです。専用のエイリアスはより短く表現力があります。このルールは自動修正に対応しています。
#誤り
import { type Result = Result.Result<string, Error> | Result.ResultAsync<string, Error> Result } from '@praha/byethrow';
// ❌ 明示的なユニオン型を使用
type type Result = Result.Result<string, Error> | Result.ResultAsync<string, Error> 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.@exampleimport { 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 <string, Error> | import Result Result .type ResultAsync<T, E> = Promise<Result.Result<T, E>>An asynchronous variant of
Result
, wrapped in a Promise.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const fetchData = async (): Result.ResultAsync<string, Error> => {
try {
const data = await fetch('...');
return { type: 'Success', value: await data.text() };
} catch (err) {
return { type: 'Failure', error: err as Error };
}
};
@categoryCore Types ResultAsync <string, Error>;#正しい
import { type Result = Result.Result<string, Error> | Promise<Result.Result<string, Error>> Result } from '@praha/byethrow';
// ✅ ResultMaybeAsyncを使用
type type Result = Result.Result<string, Error> | Promise<Result.Result<string, Error>> Result = import Result Result .type ResultMaybeAsync<T, E> = Result.Result<T, E> | Promise<Result.Result<T, E>>A result that may be either synchronous or asynchronous.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleSynchronous Case
import { Result } from '@praha/byethrow';
const result: Result.ResultMaybeAsync<number, string> = { type: 'Success', value: 10 };
@exampleAsynchronous Case
import { Result } from '@praha/byethrow';
const result: Result.ResultMaybeAsync<number, string> = Promise.resolve({ type: 'Failure', error: 'error' });
@categoryCore Types ResultMaybeAsync <string, Error>;