#no-ambiguous-error-type
Result、ResultAsync、ResultMaybeAsync のエラー型の位置に、具体的でない型を使うことを禁止します。
#ルールの詳細
Result のエラー型が曖昧だと、呼び出し側は失敗の種類を区別できず、すべてのケースを漏れなく処理することもできません。
カスタムエラーで説明しているように、代わりにドメイン固有の具体的なエラー型を使ってください。
エラー型の位置で次の型が使われていると報告されます。
unknownとany- プリミティブ型:
string、number、boolean、bigint、symbol、null、undefined objectと{}- 基底クラスの
Error
このルールは Result.Result<T, E> のような明示的な型注釈をチェックします。TypeScript が推論した型はチェックされません。
#誤った例
import { import Result Result } from '@praha/byethrow';
// ❌ unknown
type type FindUserResult = Result.Success<string> | Result.Failure<unknown> FindUserResult = 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, unknown>;
// ❌ プリミティブ型
type type ParseResult = Result.Success<number> | Result.Failure<string> ParseResult = 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 <number, string>;
// ❌ 基底クラスの Error
type type SaveResult = Promise<Result.Result<void, Error>> SaveResult = 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 <void, Error>;#正しい例
import { import Result Result } from '@praha/byethrow';
class class UserNotFoundError UserNotFoundError extends var Error: ErrorConstructor Error {
override readonly UserNotFoundError.name: "UserNotFoundError" name = 'UserNotFoundError';
}
// ✅ 具体的なエラークラス
type type FindUserResult = Result.Success<string> | Result.Failure<UserNotFoundError> FindUserResult = 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, class UserNotFoundError UserNotFoundError >;#オプション
このルールにオプションはありません。
