• English
  • prefer-result-maybe-async

    Enforces use of ResultMaybeAsync<T, E> instead of the union type Result<T, E> | ResultAsync<T, E>.

    Rule details

    ResultMaybeAsync<T, E> is an alias for Result<T, E> | ResultAsync<T, E>. Using the dedicated alias is shorter and more expressive. This rule auto-fixes violations.

    Incorrect

    import { 
    type Result = Result.Result<string, Error> | Result.ResultAsync<string, Error>
    Result
    } from '@praha/byethrow';
    // ❌ using the explicit union type
    type Result = Result.Result<string, Error> | Result.ResultAsync<string, Error>
    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, Error> |
    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.@example
    import { 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
    <string, Error>;

    Correct

    import { 
    type Result = Result.Result<string, Error> | Promise<Result.Result<string, Error>>
    Result
    } from '@praha/byethrow';
    // ✅ using ResultMaybeAsync type
    type Result = Result.Result<string, Error> | Promise<Result.Result<string, Error>>
    Result
    =
    import Result
    Result
    .
    type ResultMaybeAsync<T, E> = Result.Result<T, E> | Promise<Result.Result<T, E>>

    A result that may be either synchronous or asynchronous.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example

    Synchronous Case

    import { Result } from '@praha/byethrow';
    
    const result: Result.ResultMaybeAsync<number, string> = { type: 'Success', value: 10 };
    @example

    Asynchronous Case

    import { Result } from '@praha/byethrow';
    
    const result: Result.ResultMaybeAsync<number, string> = Promise.resolve({ type: 'Failure', error: 'error' });
    @categoryCore Types
    ResultMaybeAsync
    <string, Error>;