#no-ambiguous-success-type
Result、ResultAsync、ResultMaybeAsync の成功型の位置に、具体的でない型を使うことを禁止します。
#ルールの詳細
Result の成功型が曖昧だと、呼び出し側は成功時に何を受け取るのかわからず、自分で値をチェックしたりキャストしたりする必要があります。
代わりに具体的な型を使ってください。
成功型の位置で unknown、any、object、{} が使われていると報告されます。
値を返さない処理のための void は許可されています。
このルールは Result.Result<T, E> のような明示的な型注釈をチェックします。TypeScript が推論した型はチェックされません。
#誤った例
import { import Result Result } from '@praha/byethrow';
class class FetchError FetchError extends var Error: ErrorConstructor Error {}
// ❌ unknown
type type FetchResult1 = Result.Success<unknown> | Result.Failure<FetchError> FetchResult1 = 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 <unknown, class FetchError FetchError >;
// ❌ any
type type FetchResult2 = Result.Failure<FetchError> | Result.Success<any> FetchResult2 = 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 <any, class FetchError FetchError >;
// ❌ object
type type FetchResult3 = Result.Failure<FetchError> | Result.Success<object> FetchResult3 = 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 <object, class FetchError FetchError >;#正しい例
import { import Result Result } from '@praha/byethrow';
class class FetchError FetchError extends var Error: ErrorConstructor Error {}
// ✅ 具体的な型
type type FetchResult = Result.Success<{
id: string;
}> | Result.Failure<FetchError>
FetchResult = 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 <{ id: string id : string }, class FetchError FetchError >;
// ✅ 値を返さない処理には void
type type DeleteResult = Result.Failure<FetchError> | Result.Success<void> DeleteResult = 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 <void, class FetchError FetchError >;Tip
パースした JSON のボディのように値が本当に不明な場合は、Result.parse で検証して具体的な型を得てください。
#オプション
このルールにオプションはありません。
