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 = err<number, string>('something went wrong');
Note: passing nothing will produce a Result<T, void>
, passing undefined
will
produce a Result<T, undefined>
which is compatible with Result<T, void>
.
const normalResult = err<number, string>('oh no');
const explicitUndefined = err<number, undefined>(undefined);
const implicitVoid = err<number, void>();
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.
const arrowValidate = (data: SomeData): Result<number, string> =>
isValid(data) ? ok(42) : err('something went wrong');
function fnValidate(data: someData): Result<number, string> {
return isValid(data) ? ok(42) : err('something went wrong');
}
The value to wrap in a Result.Err
.
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 = err<number, string>('something went wrong');
Note: passing nothing will produce a Result<T, void>
, passing undefined
will
produce a Result<T, undefined>
which is compatible with Result<T, void>
.
const normalResult = err<number, string>('oh no');
const explicitUndefined = err<number, undefined>(undefined);
const implicitVoid = err<number, void>();
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.
const arrowValidate = (data: SomeData): Result<number, string> =>
isValid(data) ? ok(42) : err('something went wrong');
function fnValidate(data: someData): Result<number, string> {
return isValid(data) ? ok(42) : err('something went wrong');
}
The value to wrap in a Result.Err
.
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 will produce a
Result<T, void>
, passingundefined
will produce aResult<T, undefined>
which is compatible withResult<T, void>
.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.