Skip to content

Commit

Permalink
refactor: add exclusion when using type guard (#6168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boudewijn26 authored Mar 25, 2021
1 parent fc728cd commit 8e5f90a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions api_guard/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ export declare function pairs(n: number | bigint | boolean | ((...args: any[]) =

export declare type PartialObserver<T> = NextObserver<T> | ErrorObserver<T> | CompletionObserver<T>;

export declare function partition<T, U extends T, A>(source: ObservableInput<T>, predicate: (this: A, value: T, index: number) => value is U, thisArg: A): [Observable<U>, Observable<T>];
export declare function partition<T, U extends T>(source: ObservableInput<T>, predicate: (value: T, index: number) => value is U): [Observable<U>, Observable<T>];
export declare function partition<T, U extends T, A>(source: ObservableInput<T>, predicate: (this: A, value: T, index: number) => value is U, thisArg: A): [Observable<U>, Observable<Exclude<T, U>>];
export declare function partition<T, U extends T>(source: ObservableInput<T>, predicate: (value: T, index: number) => value is U): [Observable<U>, Observable<Exclude<T, U>>];
export declare function partition<T, A>(source: ObservableInput<T>, predicate: (this: A, value: T, index: number) => boolean, thisArg: A): [Observable<T>, Observable<T>];
export declare function partition<T>(source: ObservableInput<T>, predicate: (value: T, index: number) => boolean): [Observable<T>, Observable<T>];

Expand Down
14 changes: 13 additions & 1 deletion spec-dtslint/observables/partition-spec.ts
Original file line number Diff line number Diff line change
@@ -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<string>, Observable<string>]
Expand All @@ -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<number>]
});

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
});
Expand All @@ -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<number>, Observable<number>]
Expand Down
4 changes: 2 additions & 2 deletions src/internal/observable/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export function partition<T, U extends T, A>(
source: ObservableInput<T>,
predicate: (this: A, value: T, index: number) => value is U,
thisArg: A
): [Observable<U>, Observable<T>];
): [Observable<U>, Observable<Exclude<T, U>>];
export function partition<T, U extends T>(
source: ObservableInput<T>,
predicate: (value: T, index: number) => value is U
): [Observable<U>, Observable<T>];
): [Observable<U>, Observable<Exclude<T, U>>];

export function partition<T, A>(
source: ObservableInput<T>,
Expand Down

0 comments on commit 8e5f90a

Please sign in to comment.