From 6e1afabfd9e884d5860ecfbfec25f80fb808401e Mon Sep 17 00:00:00 2001 From: Aldwin Vlasblom Date: Sat, 24 Apr 2021 20:37:57 +0200 Subject: [PATCH] Add branch awareness and tests for the type of 'and' --- index.d.ts | 12 +++++++++++- test/types/and.test-d.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/types/and.test-d.ts 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..33665ad4 --- /dev/null +++ b/test/types/and.test-d.ts @@ -0,0 +1,34 @@ +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'); + +// 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 workaround = (fl.resolve ('a')) .pipe (fl.and (fl.never)); expectType (workaround); +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)));