From 3b772c4269998e0956eee70ab6752502abefeb5f Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Fri, 23 Oct 2020 11:10:09 -0500 Subject: [PATCH] refactor(every): Improve types, address comments - Every now infers correctly when `Boolean` constructor is used. --- api_guard/dist/types/operators/index.d.ts | 7 ++++--- spec-dtslint/operators/every-spec.ts | 7 +++++-- src/internal/operators/every.ts | 9 ++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/api_guard/dist/types/operators/index.d.ts b/api_guard/dist/types/operators/index.d.ts index 3c31d128e27..ee8f436233f 100644 --- a/api_guard/dist/types/operators/index.d.ts +++ b/api_guard/dist/types/operators/index.d.ts @@ -96,8 +96,7 @@ export declare function endWith(v1: A, v2: B, v3: C, v4: D, v5 export declare function endWith(v1: A, v2: B, v3: C, v4: D, v5: E, v6: F, scheduler: SchedulerLike): OperatorFunction; export declare function endWith(...args: A): OperatorFunction>; -export declare function every(predicate: (value: T, index: number, source: Observable) => value is S, thisArg?: any): OperatorFunction; -export declare function every(predicate: BooleanConstructor, thisArg?: any): OperatorFunction>; +export declare function every(predicate: BooleanConstructor, thisArg?: any): OperatorFunction extends never ? false : boolean>; export declare function every(predicate: (value: T, index: number, source: Observable) => boolean, thisArg?: any): OperatorFunction; export declare function exhaust(): OperatorFunction, T>; @@ -316,7 +315,9 @@ export declare function takeLast(count: number): MonoTypeOperatorFunction; export declare function takeUntil(notifier: ObservableInput): MonoTypeOperatorFunction; export declare function takeWhile(predicate: (value: T, index: number) => false, inclusive?: boolean): OperatorFunction; -export declare function takeWhile(predicate: BooleanConstructor, inclusive?: boolean): OperatorFunction extends never ? never : T>; +export declare function takeWhile(predicate: BooleanConstructor): OperatorFunction extends never ? never : T>; +export declare function takeWhile(predicate: BooleanConstructor, inclusive: false): OperatorFunction extends never ? never : T>; +export declare function takeWhile(predicate: BooleanConstructor, inclusive: true): MonoTypeOperatorFunction; export declare function takeWhile(predicate: (value: T, index: number) => value is S): OperatorFunction; export declare function takeWhile(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction; export declare function takeWhile(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction; diff --git a/spec-dtslint/operators/every-spec.ts b/spec-dtslint/operators/every-spec.ts index f6c776ba969..f313eb9f3d4 100644 --- a/spec-dtslint/operators/every-spec.ts +++ b/spec-dtslint/operators/every-spec.ts @@ -38,6 +38,9 @@ it('should expect function parameter', () => { }); it('should handle the Boolean constructor', () => { - const a = of(0 as const, '' as const, false as const, null, undefined, -0 as const, 0n as const).pipe(every(Boolean)); // $ExpectType Observable - const b = of(0 as const, '' as const, 'hi there' as const, false as const, null, undefined, -0 as const, 0n as const).pipe(every(Boolean)); // $ExpectType Observable<"hi there"> + const a = of(0 as const, '' as const, false as const, null, undefined, -0 as const, 0n as const).pipe(every(Boolean)); // $ExpectType Observable + const b = of(0 as const, '' as const, 'hi there' as const, false as const, null, undefined, -0 as const, 0n as const).pipe(every(Boolean)); // $ExpectType Observable + const c = of('test' as const, true as const, 1 as const, [], {}).pipe(every(Boolean)); // $ExpectType Observable + const d = of(NaN, NaN, NaN).pipe(every(Boolean)); // $ExpectType Observable + const e = of(0, 1, 0).pipe(every(Boolean)); // $ExpectType Observable }) \ No newline at end of file diff --git a/src/internal/operators/every.ts b/src/internal/operators/every.ts index 74657186f56..138feed3d88 100644 --- a/src/internal/operators/every.ts +++ b/src/internal/operators/every.ts @@ -1,14 +1,13 @@ /** @prettier */ import { Observable } from '../Observable'; -import { OperatorFunction, TruthyTypesOf } from '../types'; +import { Falsy, OperatorFunction } from '../types'; import { operate } from '../util/lift'; import { OperatorSubscriber } from './OperatorSubscriber'; -export function every( - predicate: (value: T, index: number, source: Observable) => value is S, +export function every( + predicate: BooleanConstructor, thisArg?: any -): OperatorFunction; -export function every(predicate: BooleanConstructor, thisArg?: any): OperatorFunction>; +): OperatorFunction extends never ? false : boolean>; export function every( predicate: (value: T, index: number, source: Observable) => boolean, thisArg?: any