Skip to content

Commit

Permalink
feat(groupBy): Add typeguards support for groupBy (#5441)
Browse files Browse the repository at this point in the history
* feat(groupBy): Add typeguards support for groupBy

* fix: use T for type guard value

* test: add dtslint tests for groupBy type guard sig

Co-authored-by: Moshe Kolodny <kolodny@github.com>
Co-authored-by: Nicholas Jamieson <nicholas@cartant.com>
  • Loading branch information
3 people authored May 17, 2020
1 parent e678e81 commit da382da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
37 changes: 36 additions & 1 deletion spec-dtslint/operators/groupBy-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { of, Subject, GroupedObservable } from 'rxjs';
import { groupBy } from 'rxjs/operators';
import { groupBy, mergeMap } from 'rxjs/operators';

it('should infer correctly', () => {
const o = of(1, 2, 3).pipe(groupBy(value => value.toString())); // $ExpectType Observable<GroupedObservable<string, number>>
Expand Down Expand Up @@ -47,3 +47,38 @@ it('should enforce types of subject selector', () => {
const o = of(1, 2, 3).pipe(groupBy(value => value.toString(), undefined, undefined, () => 'nope')); // $ExpectError
const p = of(1, 2, 3).pipe(groupBy(value => value.toString(), undefined, undefined, (value) => new Subject<string>())); // $ExpectError
});

it('should support a user-defined type guard', () => {
function isNumber(value: string | number): value is number {
return typeof value === 'number';
}
const o = of('a', 1, 'b', 2).pipe(
groupBy(isNumber),
mergeMap((group) => {
if (group.key) {
const inferred = group; // $ExpectType GroupedObservable<true, number>
return inferred;
} else {
const inferred = group; // $ExpectType GroupedObservable<false, string>
return inferred;
}
})
);
const inferred = o; // $ExpectType Observable<string | number>
});

it('should support an inline user-defined type guard', () => {
const o = of('a', 1, 'b', 2).pipe(
groupBy((value): value is number => typeof value === 'number'),
mergeMap((group) => {
if (group.key) {
const inferred = group; // $ExpectType GroupedObservable<true, number>
return inferred;
} else {
const inferred = group; // $ExpectType GroupedObservable<false, string>
return inferred;
}
})
);
const inferred = o; // $ExpectType Observable<string | number>
});
1 change: 1 addition & 0 deletions src/internal/operators/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Subject } from '../Subject';
import { OperatorFunction } from '../types';

/* tslint:disable:max-line-length */
export function groupBy<T, K extends T>(keySelector: (value: T) => value is K): OperatorFunction<T, GroupedObservable<true, K> | GroupedObservable<false, Exclude<T, K>>>;
export function groupBy<T, K>(keySelector: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>;
export function groupBy<T, K>(keySelector: (value: T) => K, elementSelector: void, durationSelector: (grouped: GroupedObservable<K, T>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, T>>;
export function groupBy<T, K, R>(keySelector: (value: T) => K, elementSelector?: (value: T) => R, durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, R>>;
Expand Down

0 comments on commit da382da

Please sign in to comment.