From 68a3ddbbf1b2813e00b1a85c1f93734a31fe1604 Mon Sep 17 00:00:00 2001 From: Boudewijn van Groos Date: Mon, 22 Mar 2021 20:15:07 +0100 Subject: [PATCH] docs(partition): add exclusion when using type guard --- api_guard/dist/types/index.d.ts | 4 ++-- spec-dtslint/observables/partition-spec.ts | 14 +++++++++++++- src/internal/observable/partition.ts | 8 ++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/api_guard/dist/types/index.d.ts b/api_guard/dist/types/index.d.ts index 623893ae482..b22354efb24 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 474c7cae629..38187739dc2 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 4d94e462c99..9711457d0b9 100644 --- a/src/internal/observable/partition.ts +++ b/src/internal/observable/partition.ts @@ -4,15 +4,15 @@ import { ObservableInput } from '../types'; import { Observable } from '../Observable'; import { innerFrom } from './from'; -export function partition( +export function partition>( source: ObservableInput, predicate: (this: A, value: T, index: number) => value is U, thisArg: A -): [Observable, Observable]; -export function partition( +): [Observable, Observable]; +export function partition>( source: ObservableInput, predicate: (value: T, index: number) => value is U -): [Observable, Observable]; +): [Observable, Observable]; export function partition( source: ObservableInput,