#no-negated-type-guards
Disallows using the negation operator (!) on Result.isSuccess() or Result.isFailure().
This rule is auto-fixable. The fix replaces the negated call with the opposite type guard.
#Rule Details
!Result.isSuccess(result) means the same as Result.isFailure(result), but the negated form takes an extra mental step to read.
Since a Result is always either a success or a failure, there is always a direct type guard you can use instead.
#Incorrect
import { import Result Result } from '@praha/byethrow';
declare const const result: Result.Result<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.@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 <string, Error>;
// ❌ Negating isSuccess
if (!import Result Result .function isSuccess<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Success<string>Type guard to check if a
Result
is a
Success
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Success', value: 10 };
if (Result.isSuccess(result)) {
console.log(result.value); // Safe access to value
}
@categoryType Guards isSuccess (const result: Result.Result<string, Error> result )) {
// handle failure
}
// ❌ Negating isFailure
if (!import Result Result .function isFailure<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Failure<Error>Type guard to check if a
Result
is a
Failure
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Failure , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
if (Result.isFailure(result)) {
console.error(result.error); // Safe access to error
}
@categoryType Guards isFailure (const result: Result.Result<string, Error> result )) {
// handle success
}#Correct
import { import Result Result } from '@praha/byethrow';
declare const const result: Result.Result<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.@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 <string, Error>;
// ✅ Using the direct type guard
if (import Result Result .function isFailure<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Failure<Error>Type guard to check if a
Result
is a
Failure
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Failure , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
if (Result.isFailure(result)) {
console.error(result.error); // Safe access to error
}
@categoryType Guards isFailure (const result: Result.Result<string, Error> result )) {
// handle failure
}
if (import Result Result .function isSuccess<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Success<string>Type guard to check if a
Result
is a
Success
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Success', value: 10 };
if (Result.isSuccess(result)) {
console.log(result.value); // Safe access to value
}
@categoryType Guards isSuccess (const result: Result.Result<string, Error> result )) {
// handle success
}#Options
This rule has no options.
