• English
  • no-ambiguous-error-type

    Disallows non-specific types in the error position of Result, ResultAsync, and ResultMaybeAsync.

    Rule details

    Using vague types like unknown, any, Error, or primitive types as the error type of a Result means callers cannot distinguish between different error cases. Always use a concrete, domain-specific error class so that error handling is exhaustive and self-documenting.

    Incorrect

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // ❌ using 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>;
    // ❌ using a primitive 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>;
    // ❌ using the base Error class 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>;

    Correct

    import { 
    type Result = Result.Success<string> | Result.Failure<NotFoundError>
    Result
    } from '@praha/byethrow';
    // ✅ using a concrete error class 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
    >;