#prefer-result-maybe-async
Enforces the use of ResultMaybeAsync<T, E> instead of the union Result<T, E> | ResultAsync<T, E>.
This rule is auto-fixable.
#Rule Details
ResultMaybeAsync<T, E> is an alias for Result<T, E> | ResultAsync<T, E>.
The alias is shorter and clearly expresses that the value may be either synchronous or asynchronous.
#Incorrect
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 };
// ❌ The explicit union
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.@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 <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.@exampleimport { 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 >;#Correct
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.@exampleSynchronous Case
import { Result } from '@praha/byethrow';
const result: Result.ResultMaybeAsync<number, string> = { type: 'Success', value: 10 };
@exampleAsynchronous 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 >;#Options
This rule has no options.
