retuple-symbols
v1.0.0-next.2
Published
A symbol used by the retuple library for implementing custom result types.
Maintainers
Readme
Retuple Symbols
Implement a custom result-like by implementing ResultLike on a class or object. An object with this implementation can be converted to a Result by the retuple library and can be used in most places where a Result is required.
Creating a custom Result
import { ResultLikeSymbol, type ResultLike } from "retuple-symbols";
class CustomResult<T> implements ResultLike<T, CustomError> {
value: T;
constructor(value: T) {
this.value = value;
}
[ResultLikeSymbol]() {
return this.value === "test"
? { ok: true as const, value: this.value }
: { ok: false as const, value: "Value was not test" };
}
}Using a custom Result
import { Result, Ok } from "retuple";
const custom = new CustomResult("test");
const result: Result<string, Error> = Result(custom);
const chain = Ok("test")
.$andThen((val) => new CustomResult(val))
.$tap((val) => console.log(`Value should be test:`, val))
.$map((val) => "not test")
.$andThen((val) => new CustomResult(val))
.$tapErr((err) => console.log(`Should be error message:`, err))
...