Skip to content

Commit

Permalink
docs(partition): add an overload for better typing (#5756)
Browse files Browse the repository at this point in the history
* docs(partition): add an overload for better typing

* refactor: add thisArg support

* test: add dtslint tests

* chore: update api_guardian

Co-authored-by: Nicholas Jamieson <[email protected]>
  • Loading branch information
tech6hutch and cartant authored Mar 18, 2021
1 parent dd307ce commit acdab5c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api_guard/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +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, 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
16 changes: 12 additions & 4 deletions spec-dtslint/observables/partition-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ it('should infer correctly', () => {
const p = partition(of('a', 'b', 'c'), () => true); // $ExpectType [Observable<string>, Observable<string>]
});

it('should accept a thisArg parameter', () => {
const o = partition(of('a', 'b', 'c'), () => true, 5); // $ExpectType [Observable<string>, Observable<string>]
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 enforce predicate', () => {
Expand All @@ -19,9 +19,17 @@ it('should enforce predicate types', () => {
const q = partition(of('a', 'b', 'c'), (value, index: string) => true); // $ExpectError
});

it('should support this', () => {
it('should support this with type guard', () => {
const thisArg = { limit: 2 };
const a = partition(of(1, 2, 3), function (val) {
const a = partition(of(1, 2, 3), function (val): val is 1 { // $ExpectType [Observable<1>, Observable<number>]
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>]
const limit = this.limit; // $ExpectType number
return val < limit;
}, thisArg);
Expand Down
10 changes: 10 additions & 0 deletions src/internal/observable/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { ObservableInput } from '../types';
import { Observable } from '../Observable';
import { innerFrom } from './from';

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>];
export function partition<T, U extends T>(
source: ObservableInput<T>,
predicate: (value: T, index: number) => value is U
): [Observable<U>, Observable<T>];

export function partition<T, A>(
source: ObservableInput<T>,
predicate: (this: A, value: T, index: number) => boolean,
Expand Down

0 comments on commit acdab5c

Please sign in to comment.