Skip to content

Commit

Permalink
fix(find): add undefined to return type (#3970)
Browse files Browse the repository at this point in the history
* fix(find): add undefined to return type

* chore(dtslint): add find tests

Closes #3969.

* chore(find): don't use mono
  • Loading branch information
cartant authored and benlesh committed Jul 29, 2018
1 parent 932f082 commit 5a6c90f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
26 changes: 26 additions & 0 deletions spec-dtslint/operators/find-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { of } from 'rxjs';
import { find } from 'rxjs/operators';

it('should support a user-defined type guard', () => {
const o = of('foo').pipe(find((s): s is 'foo' => true)); // $ExpectType Observable<"foo" | undefined>
});

it('should support a user-defined type guard that takes an index', () => {
const o = of('foo').pipe(find((s, index): s is 'foo' => true)); // $ExpectType Observable<"foo" | undefined>
});

it('should support a user-defined type guard that takes an index and the source', () => {
const o = of('foo').pipe(find((s, index, source): s is 'foo' => true)); // $ExpectType Observable<"foo" | undefined>
});

it('should support a predicate', () => {
const o = of('foo').pipe(find(s => true)); // $ExpectType Observable<string | undefined>
});

it('should support a predicate that takes an index', () => {
const o = of('foo').pipe(find((s, index) => true)); // $ExpectType Observable<string | undefined>
});

it('should support a predicate that takes an index and the source', () => {
const o = of('foo').pipe(find((s, index, source) => true)); // $ExpectType Observable<string | undefined>
});
16 changes: 6 additions & 10 deletions src/internal/operators/find.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {OperatorFunction, MonoTypeOperatorFunction} from '../types';
import {OperatorFunction} from '../types';

export function find<T, S extends T>(predicate: (value: T, index: number, source: Observable<T>) => value is S,
thisArg?: any): OperatorFunction<T, S>;
export function find<T, S extends T>(predicate: (value: T, index: number) => value is S,
thisArg?: any): OperatorFunction<T, S>;
thisArg?: any): OperatorFunction<T, S | undefined>;
export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): MonoTypeOperatorFunction<T>;
export function find<T>(predicate: (value: T, index: number) => boolean,
thisArg?: any): MonoTypeOperatorFunction<T>;
thisArg?: any): OperatorFunction<T, T | undefined>;
/**
* Emits only the first value emitted by the source Observable that meets some
* condition.
Expand Down Expand Up @@ -48,14 +44,14 @@ export function find<T>(predicate: (value: T, index: number) => boolean,
* @owner Observable
*/
export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): MonoTypeOperatorFunction<T> {
thisArg?: any): OperatorFunction<T, T | undefined> {
if (typeof predicate !== 'function') {
throw new TypeError('predicate is not a function');
}
return (source: Observable<T>) => source.lift(new FindValueOperator(predicate, source, false, thisArg));
return (source: Observable<T>) => source.lift(new FindValueOperator(predicate, source, false, thisArg)) as Observable<T | undefined>;
}

export class FindValueOperator<T> implements Operator<T, T> {
export class FindValueOperator<T> implements Operator<T, T | number | undefined> {
constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private source: Observable<T>,
private yieldIndex: boolean,
Expand Down

0 comments on commit 5a6c90f

Please sign in to comment.