For AI agents: the complete documentation index is available at /byethrow/ja/llms.txt, the full documentation bundle is available at /byethrow/ja/llms-full.txt, and this page is available as Markdown at /byethrow/ja/guide/ecosystem/linter/prefer-result-maybe-async.md.
  • 日本語
  • prefer-result-maybe-async

    ユニオン型 Result<T, E> | ResultAsync<T, E> の代わりに ResultMaybeAsync<T, E> を使うことを強制します。

    このルールは自動修正に対応しています。

    ルールの詳細

    ResultMaybeAsync<T, E> は Result<T, E> | ResultAsync<T, E> のエイリアスです。 エイリアスのほうが短く、値が同期と非同期のどちらにもなり得ることを明確に表現できます。

    誤った例

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    class
    class UserNotFoundError
    UserNotFoundError
    extends
    var Error: ErrorConstructor
    Error
    {}
    type
    type User = {
        id: string;
        name: string;
    }
    User
    = {
    id: string
    id
    : string;
    name: string
    name
    : string };
    // ❌ ユニオン型を明示的に書いている type
    type FindUser = (id: string) => Result.Result<User, UserNotFoundError> | Result.ResultAsync<User, UserNotFoundError>
    FindUser
    = (
    id: string
    id
    : string) =>
    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
    <
    type User = {
        id: string;
        name: string;
    }
    User
    ,
    class UserNotFoundError
    UserNotFoundError
    > |
    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
    <
    type User = {
        id: string;
        name: string;
    }
    User
    ,
    class UserNotFoundError
    UserNotFoundError
    >;

    正しい例

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    class
    class UserNotFoundError
    UserNotFoundError
    extends
    var Error: ErrorConstructor
    Error
    {}
    type
    type User = {
        id: string;
        name: string;
    }
    User
    = {
    id: string
    id
    : string;
    name: string
    name
    : string };
    // ✅ ResultMaybeAsync type
    type FindUser = (id: string) => Result.ResultMaybeAsync<User, UserNotFoundError>
    FindUser
    = (
    id: string
    id
    : string) =>
    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
    <
    type User = {
        id: string;
        name: string;
    }
    User
    ,
    class UserNotFoundError
    UserNotFoundError
    >;

    オプション

    このルールにオプションはありません。