From 8e5f90af351e067ae1c6d00d45432e0ae30226c5 Mon Sep 17 00:00:00 2001 From: Boudewijn van Groos Date: Thu, 25 Mar 2021 15:39:59 +0100 Subject: [PATCH] refactor: add exclusion when using type guard (#6168) --- api_guard/dist/types/index.d.ts | 4 ++-- spec-dtslint/observables/partition-spec.ts | 14 +++++++++++++- src/internal/observable/partition.ts | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/api_guard/dist/types/index.d.ts b/api_guard/dist/types/index.d.ts index 623893ae48..3255065f84 100644 --- a/api_guard/dist/types/index.d.ts +++ b/api_guard/dist/types/index.d.ts @@ -321,8 +321,8 @@ export declare function pairs(n: number | bigint | boolean | ((...args: any[]) = export declare type PartialObserver = NextObserver | ErrorObserver | CompletionObserver; -export declare function partition(source: ObservableInput, predicate: (this: A, value: T, index: number) => value is U, thisArg: A): [Observable, Observable]; -export declare function partition(source: ObservableInput, predicate: (value: T, index: number) => value is U): [Observable, Observable]; +export declare function partition(source: ObservableInput, predicate: (this: A, value: T, index: number) => value is U, thisArg: A): [Observable, Observable>]; +export declare function partition(source: ObservableInput, predicate: (value: T, index: number) => value is U): [Observable, Observable>]; export declare function partition(source: ObservableInput, predicate: (this: A, value: T, index: number) => boolean, thisArg: A): [Observable, Observable]; export declare function partition(source: ObservableInput, predicate: (value: T, index: number) => boolean): [Observable, Observable]; diff --git a/spec-dtslint/observables/partition-spec.ts b/spec-dtslint/observables/partition-spec.ts index 474c7cae62..3d6f5517c3 100644 --- a/spec-dtslint/observables/partition-spec.ts +++ b/spec-dtslint/observables/partition-spec.ts @@ -1,4 +1,4 @@ -import { of, partition } from 'rxjs'; +import { of, from, partition } from 'rxjs'; it('should infer correctly', () => { const o = partition(of('a', 'b', 'c'), (value, index) => true); // $ExpectType [Observable, Observable] @@ -9,6 +9,10 @@ it('should support a user-defined type guard', () => { const o = partition(of(1, 2, 3), (value: number): value is 1 => value === 1); // $ExpectType [Observable<1>, Observable] }); +it('should support exclusion based on the user-defined type guard', () => { + const o = partition(from([1, 2] as const), (value: number): value is 1 => value === 1); // $ExpectType [Observable<1>, Observable<2>] +}); + it('should enforce predicate', () => { const o = partition(of('a', 'b', 'c')); // $ExpectError }); @@ -27,6 +31,14 @@ it('should support this with type guard', () => { }, thisArg); }); +it('should support this with exclusion based on the user-defined type guard', () => { + const thisArg = { limit: 2 }; + const a = partition(from([1, 2] as const), function (val): val is 1 { // $ExpectType [Observable<1>, Observable<2>] + const limit = this.limit; // $ExpectType number + return val < limit; + }, thisArg); +}); + it('should support this with predicate', () => { const thisArg = { limit: 2 }; const a = partition(of(1, 2, 3), function (val) { // $ExpectType [Observable, Observable] diff --git a/src/internal/observable/partition.ts b/src/internal/observable/partition.ts index 4d94e462c9..d617a43803 100644 --- a/src/internal/observable/partition.ts +++ b/src/internal/observable/partition.ts @@ -8,11 +8,11 @@ export function partition( source: ObservableInput, predicate: (this: A, value: T, index: number) => value is U, thisArg: A -): [Observable, Observable]; +): [Observable, Observable>]; export function partition( source: ObservableInput, predicate: (value: T, index: number) => value is U -): [Observable, Observable]; +): [Observable, Observable>]; export function partition( source: ObservableInput,