diff --git a/index.d.ts b/index.d.ts index 5a64d376..f631e5ed 100644 --- a/index.d.ts +++ b/index.d.ts @@ -83,7 +83,17 @@ export interface FutureInstance extends Functor { export function after(duration: number): (value: R) => Resolved /** Logical and for Futures. See https://github.com/fluture-js/Fluture#and */ -export function and(left: FutureInstance): (right: FutureInstance) => FutureInstance +export const and: { + (second: F extends Never ? S : never): (first: F) => Never + (second: F extends Resolved ? S : never): (first: F) => S + (second: F extends Rejected ? S : never): (first: F) => F + + (second: Uncertain): { + (first: Rejected): Rejected + (first: Resolved): Uncertain + (first: Uncertain): Uncertain + } +} /** Logical or for Futures. See https://github.com/fluture-js/Fluture#alt */ export const alt: { diff --git a/test/types/and.test-d.ts b/test/types/and.test-d.ts new file mode 100644 index 00000000..8e8a5c3c --- /dev/null +++ b/test/types/and.test-d.ts @@ -0,0 +1,36 @@ +import {expectType, expectError} from 'tsd'; + +import * as fl from '../../index.js'; + +const fsn: fl.Uncertain = fl.resolve (42); +const fns: fl.Uncertain = fl.resolve ('a'); + +const x = fl.and (fl.reject ('a')) + +// Standard usage on Future instances. +expectType (fl.and (fl.never) (fl.never)); +expectType (fl.and (fl.never) (fl.resolve ('a'))); +expectType (fl.and (fl.reject ('a')) (fl.never)); +expectType (fl.and (fl.resolve ('a')) (fl.never)); +expectType> (fl.and (fl.reject ('a')) (fl.resolve (42))); +expectType> (fl.and (fl.resolve (42)) (fl.resolve (42))); +expectType> (fl.and (fl.reject (42)) (fl.reject (42))); +expectType> (fl.and (fsn) (fsn)); +expectType> (fl.and (fl.never) (fl.reject ('a'))); +expectType> (fl.and (fl.resolve (42)) (fl.reject ('a'))); +expectType> (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) .pipe (fl.and (fl.never))); +const _24 = (fl.resolve ('a')) .pipe (fl.and (fl.never)); expectType (_24); +expectType ((fl.never) .pipe (fl.and (fl.reject ('a')))); +expectType ((fl.never) .pipe (fl.and (fl.resolve ('a')))); +expectType> ((fl.resolve (42)) .pipe (fl.and (fl.reject ('a')))); +expectType> ((fl.resolve (42)) .pipe (fl.and (fl.resolve (42)))); +expectType> ((fl.reject (42)) .pipe (fl.and (fl.reject (42)))); +expectType> ((fsn) .pipe (fl.and (fsn))); +expectType> ((fl.reject ('a')) .pipe (fl.and (fl.never))); +expectType> ((fl.reject ('a')) .pipe (fl.and (fl.resolve (42)))); +expectType> ((fl.reject (42)) .pipe (fl.and (fl.reject ('a')))); +expectError ((fns) .pipe (fl.and (fsn)));