Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(find): add undefined to return type #3970

Merged
merged 3 commits into from
Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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