#Function: toResult()
toResult<
E>(err): <O>(option) =>trueextendsHasPromise<O> ?ResultAsync<InferSome<O>,E> :Result<InferSome<O>,E>
Converts an Option or OptionAsync into a Result from
@praha/byethrow.
A Some becomes a Success; a None becomes a Failure containing err.
#Type Parameters
#E
E
The error type for a Failure.
#Parameters
#err
E
#Returns
<O>(option) => true extends HasPromise<O> ? ResultAsync<InferSome<O>, E> : Result<InferSome<O>, E>
#Examples
Some Case
import { import Option Option } from 'byenull';
const const result: Result<number, Error> result = import Option Option .function pipe<Option.Option<number>, Result<number, Error>>(a: Option.Option<number>, ab: (a: Option.Option<number>) => Result<number, Error>): Result<number, Error> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Option Option .function some<number>(value: number): Option.Option<number>Creates a
Some
from a given value.
Passing null or undefined returns
None
instead.
@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (2),
import Option Option .function toResult<Error>(err: Error): <O>(option: O) => true extends Option.HasPromise<O> ? ResultAsync<Option.InferSome<O>, Error> : Result<Option.InferSome<O>, Error>Converts an
Option
or
OptionAsync
into a Result from
https://github.com/praha-inc/byethrow @praha/byethrow
.
A
Some
becomes a Success; a
None
becomes a Failure containing err.
@typeParamE - The error type for a Failure.@exampleSome Case
import { Option } from 'byenull';
const result = Option.pipe(
Option.some(2),
Option.toResult(new Error('No value')),
);
// { type: 'Success', value: 2 }
@exampleNone Case
import { Option } from 'byenull';
const result = Option.pipe(
Option.none(),
Option.toResult(new Error('No value')),
);
// { type: 'Failure', error: Error('No value') }
@seefromResult - For the opposite direction.@categoryInterop toResult (new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('No value')),
);
// { type: 'Success', value: 2 }None Case
import { import Option Option } from 'byenull';
const const result: Result<never, Error> result = import Option Option .function pipe<Option.Option<never>, Result<never, Error>>(a: Option.Option<never>, ab: (a: Option.Option<never>) => Result<never, Error>): Result<never, Error> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Option Option .function none(): Option.Option<never>Creates a
None
: an Option that holds no value.
@returnsA None.@exampleimport { Option } from 'byenull';
const option = Option.none();
// { type: 'None' }
@categoryCreators none (),
import Option Option .function toResult<Error>(err: Error): <O>(option: O) => true extends Option.HasPromise<O> ? ResultAsync<Option.InferSome<O>, Error> : Result<Option.InferSome<O>, Error>Converts an
Option
or
OptionAsync
into a Result from
https://github.com/praha-inc/byethrow @praha/byethrow
.
A
Some
becomes a Success; a
None
becomes a Failure containing err.
@typeParamE - The error type for a Failure.@exampleSome Case
import { Option } from 'byenull';
const result = Option.pipe(
Option.some(2),
Option.toResult(new Error('No value')),
);
// { type: 'Success', value: 2 }
@exampleNone Case
import { Option } from 'byenull';
const result = Option.pipe(
Option.none(),
Option.toResult(new Error('No value')),
);
// { type: 'Failure', error: Error('No value') }
@seefromResult - For the opposite direction.@categoryInterop toResult (new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('No value')),
);
// { type: 'Failure', error: Error('No value') }#See
fromResult - For the opposite direction.