T The type of the item contained in the Result
.
Create an instance of Err
.
If you need to create an instance with a specific type (as you do whenever you are not constructing immediately for a function return or as an argument to a function), you can use a type parameter:
const notString = Result.err<number, string>('something went wrong');
Note: passing nothing, or passing null
or undefined
explicitly, will
produce a Result<T, Unit>
, rather than producing the nonsensical and in
practice quite annoying Result<null, string>
etc. See Unit
for
more.
const normalResult = Result.err<number, string>('oh no');
const explicitUnit = Result.err<number, Unit>(Unit);
const implicitUnit = Result.err<number, Unit>();
In the context of an immediate function return, or an arrow function with a single expression value, you do not have to specify the types, so this can be quite convenient.
type SomeData = {
//...
};
const isValid = (data: SomeData): boolean => {
// true or false...
}
const arrowValidate = (data: SomeData): Result<number, Unit> =>
isValid(data) ? Result.ok(42) : Result.err();
function fnValidate(data: someData): Result<number, Unit> {
return isValid(data) ? Result.ok(42) : Result.err();
}
T The type of the item contained in the Result
.
Generated using TypeDoc
Create an instance of
Err
.If you need to create an instance with a specific type (as you do whenever you are not constructing immediately for a function return or as an argument to a function), you can use a type parameter:
Note: passing nothing, or passing
null
orundefined
explicitly, will produce aResult<T, Unit>
, rather than producing the nonsensical and in practice quite annoyingResult<null, string>
etc. SeeUnit
for more.In the context of an immediate function return, or an arrow function with a single expression value, you do not have to specify the types, so this can be quite convenient.