Readonly
error💬 The error message that was given in Result.fail(error)
💬 Chains multiple callbacks (which return Results) together (on this Result), where every previous value is forwarded to the next callback.
// every validate*** returns a Result<number>:
const veryValid: Result<number> = this.validateNumber(420).chain(
(valid) => this.validateIntegerAndRound(valid),
(valid) => this.validateInterval(valid, {min: 69})
);
Rest
...members: F[]to chain together
💬 Converts the value
of this Result to another Result, if this Result is successful.
Returns this failed Result otherwise.
// Result<number> => Result<Integer>
this.validate(420).convertTo((valid) => new Integer(valid));
for the conversion of the value
Protected
get💬 Indicates if this Result was successful and converts it to reveal the getValue()
method.
getValue()
method, which returns the actual value, you need to check if the result was successful.Result.fail('message').getValue()
throws an Error
because there is no value!)is Result+getValue()
- i.e. after checking, getValue()
is "activated".true if the Result is successful + activation of the getValue()
method on the Result
const result = Result.ok('test');
result.getValue(); // ❌ TS: Property 'getValue' is protected and only accessible ...
if (result.isSuccess()) {
result.getValue(); // ✅ fine
} else {
result.getValue(); // ❌ same
}
💬 Calls the given callback
if the Result fails.
error
for the callback
onSuccess()
methodInteger.validate(42)
.onFail((error) => console.error(error))
.onSuccess((value) => console.log(Yes ${value} is an integer
));
to be called if this Result fails
💬 Calls the given callback
if the Result
is successful.
value
for the callback
onFail()
methodInteger.validate(42)
.onSuccess((value) => console.log(Yes ${value} is an integer
));
.onFail((error) => console.error(error))
to be called if this Result
is successful
Static
combine💬 Combines multiple Results in a map, checks every Result and:
Result
with a Record
of all Results' valuesResult
with the error of the first failed Resultconst book = Result.combine({
title: Result.ok('MyBook'),
author: Result.ok('My Self'),
})
book.getValue() // === {title: 'MyBook', author: 'My Self'}
a Record
of multiple Result
s
Static
failStatic
okGenerated using TypeDoc
The Result of a validation, either successful or failed.
isSuccess
=> has a value (getValue()
)!isSuccess
=> has anerror
(string) andthrows Error
whengetValue()
is calledgetValue()
method, which returns the vaule ofResult.ok(value)
, you need to check if the result was successful by callingisSuccess()
.