#prefer-result-async
Promise<Result<T, E>> の代わりに ResultAsync<T, E> を使うことを強制します。
このルールは自動修正に対応しています。
#ルールの詳細
ResultAsync<T, E> は Promise<Result<T, E>> のエイリアスです。
エイリアスのほうが短く、関数が非同期の Result を返すことがひと目でわかります。
#誤った例
import { import Result Result } from '@praha/byethrow';
class class UserNotFoundError UserNotFoundError extends var Error: ErrorConstructor Error {}
type type User = {
id: string;
name: string;
}
User = { id: string id : string; name: string name : string };
// ❌ Promise<Result<...>>
declare const const findUser: (id: string) => Promise<Result.Result<User, UserNotFoundError>> findUser : (id: string id : string) => interface Promise<T>Represents the completion of an asynchronous operation
Promise <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 <type User = {
id: string;
name: string;
}
User , class UserNotFoundError UserNotFoundError >>;#正しい例
import { import Result Result } from '@praha/byethrow';
class class UserNotFoundError UserNotFoundError extends var Error: ErrorConstructor Error {}
type type User = {
id: string;
name: string;
}
User = { id: string id : string; name: string name : string };
// ✅ ResultAsync
declare const const findUser: (id: string) => Result.ResultAsync<User, UserNotFoundError> findUser : (id: string id : string) => 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 <type User = {
id: string;
name: string;
}
User , class UserNotFoundError UserNotFoundError >;#オプション
このルールにオプションはありません。
