A Result type inspired by Rust and Kotlin that leverages TypeScript's powerful type system to simplify error handling and make your code more readable and maintainable with full type safety.
Sometimes, I use SomeType | Result<Error, SomeType> as a parameter to function and I don't have a reliable way to check that value is a Result. To write code like this:
function doSomething(value: CustomType | Result<Error, CustomType>) {
let type: SomeType;
if (isCustomType(value)) {
type = value;
} else if (isResult(value)) {
if (value.isFailure()) {
return value;
}
type = value.value;
} else {
return Result.error(new Error(`Unexpected value ${value}`));
}
// some other logic
}
Or maybe something like isFailure(result) and isSuccess(result) would be more helpful, so I could write without nesting:
function doSomething(value: CustomType | Result<Error, CustomType>) {
let type: SomeType;
if (isCustomType(value)) {
type = value;
} else if (isSuccess(value)) {
type = value.value;
} else if (isFailure(value)) {
return value;
} else {
return Result.error(new Error(`Unexpected value ${value}`));
}
// some other logic
}
By the way, very nice library! I enjoy using it, thanks!
Sometimes, I use
SomeType | Result<Error, SomeType>
as a parameter to function and I don't have a reliable way to check that value is aResult
. To write code like this:Or maybe something like
isFailure(result)
andisSuccess(result)
would be more helpful, so I could write without nesting:By the way, very nice library! I enjoy using it, thanks!