tiinvo

tiinvo / Exports / Result

Namespace: Result

Table of contents

Type Aliases

Factories

Guardables

Comparables

Filterables

Functions

Type Aliases

Err

Ƭ Err: Error

Represents an error

Defined in

src/Result.ts:9


Ok

Ƭ Ok<A>: A extends Error ? never : A

Represents the successful result of an operation

Type parameters

Name
A

Defined in

src/Result.ts:13


T

Ƭ T<A>: Ok<A> | Err

Could represent both an Error Err or a successful result of an operation Ok<a>

Type parameters

Name
A

Defined in

src/Result.ts:17

Factories

err

err(a): Error

Returns an Err

Example

import { Result } from 'tiinvo';

Result.err(10)                    instanceof Error // true
Result.err(new TypeError('aaaa')) instanceof Error // true
Result.err({})                    instanceof Error // true
Result.err(10n)                   instanceof Error // true
Result.err([10, 20, 30])          instanceof Error // true

Since

3.8.0

Parameters

Name Type
a unknown

Returns

Error

Defined in

src/Result.ts:39

Guardables

isErr

isErr(x): x is Error

Checks if a value is Err

Example

import { Result } from 'tiinvo';

Result.isErr(10)                    // false
Result.isErr(new TypeError('aaaa')) // true

Since

4.0.0

Parameters

Name Type Description
x unknown the value to check

Returns

x is Error

true if x is Err, false otherwise

Defined in

src/Result.ts:76


isOk

isOk<A>(x): x is Ok<A>

Checks if a value is Ok

Example

import { Result } from 'tiinvo';

Result.isOk(10)                    // true
Result.isOk(new TypeError('aaaa')) // false

Since

4.0.0

Type parameters

Name
A

Parameters

Name Type
x T<A>

Returns

x is Ok<A>

true if x is Ok<A>, false otherwise

Defined in

src/Result.ts:95


isOkOf

isOkOf<A>(g): (x: unknown) => x is A

Checks if a value is Ok<A>

import { Num, Result } from 'tiinvo';

const guard = Result.isOkOf(Num.guard);

guard(10)                    // true
guard("hello")               // false
guard(new TypeError('aaaa')) // false

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
g Guardable<A> the type guard

Returns

fn

the Guard

▸ (x): x is A

Parameters
Name Type
x unknown
Returns

x is A

Defined in

src/Result.ts:116

Comparables

cmp

cmp<A>(cmp, a, b): ComparableResult

Compares two results T<A> by a given Comparable<A>.

Example

import { Str, Result } from 'tiinvo';

Result.cmp(Str.cmp, "a", "a")                    // 0
Result.cmp(Str.cmp, "a", "b")                    // -1
Result.cmp(Str.cmp, "b", "a")                    // 1
Result.cmp(Str.cmp, new Error(), new Error())    // 0
Result.cmp(Str.cmp, new Error(), "a")            // -1
Result.cmp(Str.cmp, "a", new Error())            // 1

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
cmp Comparable<A> the comparer function
a T<A> first value
b T<A> last value

Returns

ComparableResult

Defined in

src/Result.ts:150

cmp<A>(cmp, a, b): ComparableResult

Compares two results T<A> by a given ComparableModule<A>.

Example

import { Str, Result } from 'tiinvo';

Result.cmp(Str, "a", "a")                    // 0
Result.cmp(Str, "a", "b")                    // -1
Result.cmp(Str, "b", "a")                    // 1
Result.cmp(Str, new Error(), new Error())    // 0
Result.cmp(Str, new Error(), "a")            // -1
Result.cmp(Str, "a", new Error())            // 1

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
cmp ComparableModule<A> the comparer function
a T<A> first value
b T<A> last value

Returns

ComparableResult

Defined in

src/Result.ts:179

cmp<A>(cmp, a): Unary<T<A>, ComparableResult>

Returns a unary function which compares two results T<A> by a given Comparable<A>.

If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1

Example

import { Str, Result } from 'tiinvo';

const cmp = Result.cmp(Str.cmp, "a");

cmp("a")                    // 0
cmp("b")                    // -1
cmp("a")                    // 1
cmp(new Error())            // 1

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
cmp Comparable<A> the comparer function
a T<A> first value

Returns

Unary<T<A>, ComparableResult>

the unary function which returns

Defined in

src/Result.ts:208

cmp<A>(cmp, a): Unary<T<A>, ComparableResult>

Returns a unary function which compares two results T<A> by a given ComparableModule<A>.

If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1

Example

import { Str, Result } from 'tiinvo';

const cmp = Result.cmp(Str, "a");

cmp("a")                    // 0
cmp("b")                    // -1
cmp("a")                    // 1
cmp(new Error())            // 1

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
cmp ComparableModule<A> the comparer module
a T<A> first value

Returns

Unary<T<A>, ComparableResult>

the unary function which returns

Defined in

src/Result.ts:237

cmp<A>(cmp): Binary<T<A>, T<A>, ComparableResult>

Returns a binary function which compares two results T<A> by a given Comparable<A>.

If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1

Example

import { Str, Result } from 'tiinvo';

const cmp = Result.cmp(Str.cmp);

cmp("a", "a")                    // 0
cmp("a", "b")                    // -1
cmp("b", "a")                    // 1
cmp("a", new Error())            // 1
cmp(new Error(), "a")            // -1
cmp(new Error(), new Error())    // 0

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
cmp Comparable<A> the comparer function

Returns

Binary<T<A>, T<A>, ComparableResult>

the binary function which returns

Defined in

src/Result.ts:267

cmp<A>(cmp): Binary<T<A>, T<A>, ComparableResult>

Returns a binary function which compares two results T<A> by a given ComparableModule<A>.

If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1

Example

import { Str, Result } from 'tiinvo';

const cmp = Result.cmp(Str);

cmp("a", "a")                    // 0
cmp("a", "b")                    // -1
cmp("b", "a")                    // 1
cmp("a", new Error())            // 1
cmp(new Error(), "a")            // -1
cmp(new Error(), new Error())    // 0

Since

4.0.0

Type parameters

Name Description
A the type

Parameters

Name Type Description
cmp ComparableModule<A> the comparer module

Returns

Binary<T<A>, T<A>, ComparableResult>

the binary function which returns

Defined in

src/Result.ts:297


eq

eq<A>(eq, a, b): boolean

Returns true if two results are equal, false otherwise.

import { Num, Result } from 'tiinvo';

Result.eq(Num.eq, 0, 0)                         // true
Result.eq(Num.eq, new Error(), new TypeError()) // true
Result.eq(Num.eq, new Error(), 0)               // false
Result.eq(Num.eq, 0, new Error())               // false
Result.eq(Num.eq, 1_000_000, 0)                 // false

Since

4.0.0

Type parameters

Name Description
A the value type

Parameters

Name Type Description
eq Equatable<A> the Equatable functor
a T<A> the left compared value
b T<A> the right compared value

Returns

boolean

true if a equals to b

Defined in

src/Result.ts:349

eq<A>(eq, a, b): boolean

Returns true if two results are equal, false otherwise.

import { Num, Result } from 'tiinvo';

Result.eq(Num, 0, 0)                         // true
Result.eq(Num, new Error(), new TypeError()) // true
Result.eq(Num, new Error(), 0)               // false
Result.eq(Num, 0, new Error())               // false
Result.eq(Num, 1_000_000, 0)                 // false

Since

4.0.0

Type parameters

Name Description
A the value type

Parameters

Name Type Description
eq EquatableModule<A> the Equatable module
a T<A> the left compared value
b T<A> the right compared value

Returns

boolean

true if a equals to b

Defined in

src/Result.ts:371

eq<A>(eq, a): Unary<T<A>, boolean>

Returns a unary function which returns true if two results are equal, false otherwise.

import { Num, Result } from 'tiinvo';

const is0 = Result.eq(Num.eq, 0)

Result.eq(0)                         // true
Result.eq(new TypeError())           // false
Result.eq(new Error())               // false
Result.eq(1_000_000)                 // false

Since

4.0.0

Type parameters

Name Description
A the value type

Parameters

Name Type Description
eq Equatable<A> the Equatable functor
a T<A> the left compared value

Returns

Unary<T<A>, boolean>

the unary function

Defined in

src/Result.ts:393

eq<A>(eq, a): Unary<T<A>, boolean>

Returns a unary function which returns true if two results are equal, false otherwise.

import { Num, Result } from 'tiinvo';

const is0 = Result.eq(Num, 0)

Result.eq(0)                         // true
Result.eq(new TypeError())           // false
Result.eq(new Error())               // false
Result.eq(1_000_000)                 // false

Since

4.0.0

Type parameters

Name Description
A the value type

Parameters

Name Type Description
eq EquatableModule<A> the Equatable functor
a T<A> the left compared value

Returns

Unary<T<A>, boolean>

the unary function

Defined in

src/Result.ts:415

eq<a>(eq): Binary<T<a>, T<a>, boolean>

Returns a binary function which returns true if two results are equal, false otherwise.

import { Num, Result } from 'tiinvo';

const is0 = Result.eq(Num.eq)

Result.eq(0, 0)                         // true
Result.eq(new TypeError(), new Error()) // true
Result.eq(0, new Error())               // false
Result.eq(1_000, 1_000)                 // true

Since

4.0.0

Type parameters

Name
a

Parameters

Name Type Description
eq Equatable<a> the Equatable functor

Returns

Binary<T<a>, T<a>, boolean>

the binary function

Defined in

src/Result.ts:436

eq<a>(eq): Binary<T<a>, T<a>, boolean>

Returns a binary function which returns true if two results are equal, false otherwise.

import { Num, Result } from 'tiinvo';

const is0 = Result.eq(Num)

Result.eq(0, 0)                         // true
Result.eq(new TypeError(), new Error()) // true
Result.eq(0, new Error())               // false
Result.eq(1_000, 1_000)                 // true

Since

4.0.0

Type parameters

Name
a

Parameters

Name Type Description
eq EquatableModule<a> the Equatable module

Returns

Binary<T<a>, T<a>, boolean>

the binary function

Defined in

src/Result.ts:457

Filterables

filter

filter<A>(f, a): T<A>

Returns Ok<A> if the value a is Ok<A> and the predicate is satisfied, otherwise returns Err.

import { Result, Num } from 'tiinvo';

Result.filter(Num.gt(1), 1)               // Error("Value did not pass filter")
Result.filter(Num.gt(1), 2)               // 2
Result.filter(Num.gt(1), new TypeError()) // Error("Value did not pass filter")

Since

4.0.0

Type parameters

Name Description
A the value type

Parameters

Name Type Description
f Filterable<A> the Filterable functor
a T<A> the value to filter

Returns

T<A>

T<A> if the Filterable has been satisfied

Defined in

src/Result.ts:505

filter<A>(f): Unary<T<A>, T<A>>

Returns a unary function which checks if Result.T<A> is Ok<A> and the predicate is satisfied, otherwise returns Err.

import { Result, Num } from 'tiinvo';

const f = Result.filter(Num.gt(1));

f(1)               // Error("Value did not pass filter")
f(2)               // 2
f(new TypeError()) // Error("Value did not pass filter")

Since

4.0.0

Type parameters

Name Description
A the value type

Parameters

Name Type Description
f Filterable<A> the Filterable functor

Returns

Unary<T<A>, T<A>>

the unary function

Defined in

src/Result.ts:525

Functions

map

map<A, B>(m): (a: T<A>) => T<B>

Maps Result.t<a> to Result.t<b> if ok, otherwise returns err

import { Num, Result } from 'tiinvo';

const m = Result.map(Num.add(10))

m(10)                   // 20
m(new Error('foobar!')) // Error('foobar!')

Since

4.0.0

Type parameters

Name
A
B

Parameters

Name Type Description
m Mappable<A, B> the Mappable functor

Returns

fn

▸ (a): T<B>

Parameters
Name Type
a T<A>
Returns

T<B>

Defined in

src/Result.ts:557


mapOr

mapOr<a, b>(m, b): (a: T<a>) => b

Maps Result.t<a> to Result.t<b> if ok, otherwise returns b

import { Str, Result } from 'tiinvo';

const map = Result.mapOr(Str.length, 0);

map('hello')        // 5
map(new Error())    // 0

Since

4.0.0

Type parameters

Name
a
b

Parameters

Name Type
m Mappable<a, b>
b b

Returns

fn

▸ (a): b

Parameters
Name Type
a T<a>
Returns

b

Defined in

src/Result.ts:574


tryAsync

tryAsync<f>(f): (…args: Parameters<f>) => Promise<T<ReturnType<f>>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => Promise<T<ReturnType<f>>>

Calls a function f with it’s arguments and returns a Promise<Result.t<ReturnType<f>>>

import { Result, Num } from 'tiinvo';

const fn = async (arg: number) => {
  if (Num.isEven(arg)) {  
    return arg * 2;  
  }
 
  throw new Error(`${arg} is not even`);
}

const safe = Result.tryAsync(fn);

await safe(2) // 4
await safe(3) // Error("3 is not even")

Result.isOk(await safe(2))  // true
Result.isErr(await safe(3)) // true

Since

4.0.0

Type parameters

Name Type
f extends AnyAsyncFn

Parameters

Name Type
f f

Returns

(…args: Parameters<f>) => Promise<T<ReturnType<f>>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => Promise<T<ReturnType<f>>>

Defined in

src/Result.ts:607


trySync

trySync<f>(f): (…args: Parameters<f>) => T<ReturnType<f>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => T<ReturnType<f>>

Calls a function f with it’s arguments and returns a Promise<Result.t<ReturnType<f>>>

import { Result, Num } from 'tiinvo';

const fn = (arg: number) => {
  if (Num.isEven(arg)) {  
    return arg * 2;  
  }
 
  throw new Error(`${arg} is not even`);
}

const safe = Result.trySync(fn);

safe(2) // 4
safe(3) // Error("3 is not even")

Result.isOk(safe(2))  // true
Result.isErr(safe(3)) // true

Since

4.0.0

Type parameters

Name Type
f extends AnyFn

Parameters

Name Type
f f

Returns

(…args: Parameters<f>) => T<ReturnType<f>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => T<ReturnType<f>>

Defined in

src/Result.ts:647