Skip to content

Commit

Permalink
Add branch awareness and tests for the type of 'and'
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Apr 24, 2021
1 parent 7072818 commit f171ef5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ export interface FutureInstance<L, R> extends Functor<R> {
export function after(duration: number): <R>(value: R) => Resolved<R>

/** Logical and for Futures. See https://github.com/fluture-js/Fluture#and */
export function and<L, R>(left: FutureInstance<L, R>): (right: FutureInstance<L, any>) => FutureInstance<L, R>
export const and: {
<F extends AnyFuture, S extends AnyFuture>(second: F extends Never ? S : never): (first: F) => Never
<F extends AnyFuture, S extends AnyFuture>(second: F extends Resolved<unknown> ? S : never): (first: F) => S
<F extends AnyFuture, S extends AnyFuture>(second: F extends Rejected<unknown> ? S : never): (first: F) => F

<L, R>(second: Uncertain<L, R>): {
<T>(first: Rejected<T>): Rejected<T>
(first: Resolved<any>): Uncertain<L, R>
(first: Uncertain<L, any>): Uncertain<L, R>
}
}

/** Logical or for Futures. See https://github.com/fluture-js/Fluture#alt */
export const alt: {
Expand Down
36 changes: 36 additions & 0 deletions test/types/and.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {expectType, expectError} from 'tsd';

import * as fl from '../../index.js';

const fsn: fl.Uncertain<string, number> = fl.resolve (42);
const fns: fl.Uncertain<number, string> = fl.resolve ('a');

const x = fl.and (fl.reject ('a'))

// Standard usage on Future instances.
expectType<fl.Never> (fl.and (fl.never) (fl.never));
expectType<fl.Never> (fl.and (fl.never) (fl.resolve ('a')));
expectType<fl.Never> (fl.and (fl.reject ('a')) (fl.never));
expectType<fl.Never> (fl.and (fl.resolve ('a')) (fl.never));
expectType<fl.Rejected<string>> (fl.and (fl.reject ('a')) (fl.resolve (42)));
expectType<fl.Resolved<number>> (fl.and (fl.resolve (42)) (fl.resolve (42)));
expectType<fl.Rejected<number>> (fl.and (fl.reject (42)) (fl.reject (42)));
expectType<fl.Uncertain<string, number>> (fl.and (fsn) (fsn));
expectType<fl.Rejected<string>> (fl.and (fl.never) (fl.reject ('a')));
expectType<fl.Rejected<string>> (fl.and (fl.resolve (42)) (fl.reject ('a')));
expectType<fl.Rejected<number>> (fl.and (fl.reject ('a')) (fl.reject (42)));
expectError (fl.and (fsn) (fns));

// Usage with pipe on Future instances (https://git.io/JLx3F).
expectType<fl.Never> ((fl.never) .pipe (fl.and (fl.never)));
const _24 = (fl.resolve ('a')) .pipe (fl.and (fl.never)); expectType<fl.Never> (_24);
expectType<fl.Never> ((fl.never) .pipe (fl.and (fl.reject ('a'))));
expectType<fl.Never> ((fl.never) .pipe (fl.and (fl.resolve ('a'))));
expectType<fl.Rejected<string>> ((fl.resolve (42)) .pipe (fl.and (fl.reject ('a'))));
expectType<fl.Resolved<number>> ((fl.resolve (42)) .pipe (fl.and (fl.resolve (42))));
expectType<fl.Rejected<number>> ((fl.reject (42)) .pipe (fl.and (fl.reject (42))));
expectType<fl.Uncertain<string, number>> ((fsn) .pipe (fl.and (fsn)));
expectType<fl.Rejected<string>> ((fl.reject ('a')) .pipe (fl.and (fl.never)));
expectType<fl.Rejected<string>> ((fl.reject ('a')) .pipe (fl.and (fl.resolve (42))));
expectType<fl.Rejected<number>> ((fl.reject (42)) .pipe (fl.and (fl.reject ('a'))));
expectError ((fns) .pipe (fl.and (fsn)));

0 comments on commit f171ef5

Please sign in to comment.