• 日本語
  • no-ambiguous-error-type

    Result / ResultAsync / ResultMaybeAsync のエラー型に曖昧な型を使用することを禁止します。

    ルールの詳細

    unknownanyError・プリミティブ型などの曖昧な型をエラー型に使用すると、呼び出し元がエラーの種類を区別できなくなります。具体的なドメイン固有のエラークラスを使用することで、エラーハンドリングを網羅的かつ自己文書化した形にできます。

    誤り

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // ❌ unknownを使用 type
    type Result1 = Result.Success<string> | Result.Failure<unknown>
    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.@example
    import { 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 Result2 = Result.Success<string> | Result.Failure<string>
    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.@example
    import { 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, string>;
    // ❌ 基底クラスのErrorを使用 type
    type Result3 = Result.Success<string> | Result.Failure<Error>
    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.@example
    import { 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 { 
    type Result = Result.Success<string> | Result.Failure<NotFoundError>
    Result
    } from '@praha/byethrow';
    // ✅ 具体的なエラークラスを使用 class
    class NotFoundError
    NotFoundError
    extends
    var Error: ErrorConstructor
    Error
    {}
    type
    type Result = Result.Success<string> | Result.Failure<NotFoundError>
    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.@example
    import { 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 NotFoundError
    NotFoundError
    >;