#no-ambiguous-success-type
Result / ResultAsync / ResultMaybeAsync の成功型に曖昧な型を使用することを禁止します。
#ルールの詳細
unknown・any・object・{} などの曖昧な型を成功型に使用すると、呼び出し元が受け取る値の情報が隠されます。具体的な型を使用することで、成功時に何を受け取るかを明確にできます。
#誤り
import { import Result Result } from '@praha/byethrow';
// ❌ unknownを使用
type type Result1 = Result.Success<unknown> | Result.Failure<Error> Result1 = 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, Error>;
// ❌ anyを使用
type type Result2 = Result.Failure<Error> | Result.Success<any> Result2 = 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, Error>;
// ❌ objectを使用
type type Result3 = Result.Failure<Error> | Result.Success<object> Result3 = 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, Error>;#正しい
import { import Result Result } from '@praha/byethrow';
// ✅ 具体的な型を使用
type type Result1 = Result.Success<{
id: string;
}> | Result.Failure<Error>
Result1 = 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 }, Error>;
// ✅ 値を返さない処理ではvoidを使用
type type Result2 = Result.Failure<Error> | Result.Success<void> Result2 = 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, Error>;