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(first/last): fix type-guard signatures #3952

Merged
merged 8 commits into from
Jul 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions compat/operator/first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Observable } from 'rxjs';
import { first as higherOrder } from 'rxjs/operators';

/* tslint:disable:max-line-length */
export function first<T>(this: Observable<T>, predicate?: null, defaultValue?: T): Observable<T>;
export function first<T, S extends T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue?: T): Observable<S>;
export function first<T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: T): Observable<T>;
export function first<T, D = T>(this: Observable<T>, predicate?: null, defaultValue?: D): Observable<T | D>;
export function first<T, S extends T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue?: S): Observable<S>;
export function first<T, D = T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: D): Observable<T | D>;
/* tslint:enable:max-line-length */

/**
Expand Down
6 changes: 3 additions & 3 deletions compat/operator/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Observable } from 'rxjs';
import { last as higherOrder } from 'rxjs/operators';

/* tslint:disable:max-line-length */
export function last<T>(this: Observable<T>, predicate?: null, defaultValue?: T): Observable<T>;
export function last<T, S extends T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue?: T): Observable<S>;
export function last<T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: T): Observable<T>;
export function last<T, D = T>(this: Observable<T>, predicate?: null, defaultValue?: D): Observable<T | D>;
export function last<T, S extends T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue?: S): Observable<S>;
export function last<T, D = T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: D): Observable<T | D>;
/* tslint:enable:max-line-length */
/**
* Returns an Observable that emits only the last item emitted by the source Observable.
Expand Down
64 changes: 64 additions & 0 deletions spec-dtslint/operators/first-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { of } from 'rxjs';
import { first } from 'rxjs/operators';

const isFooBar = (value: string): value is 'foo' | 'bar' => /^(foo|bar)$/.test(value);

it('should support an undefined predicate with no default', () => {
const o = of('foo').pipe(first(undefined)); // $ExpectType Observable<string>
});

it('should support an undefined predicate with a T default', () => {
const o = of('foo').pipe(first(undefined, 'bar')); // $ExpectType Observable<string>
});

it('should support an undefined predicate with a non-T default', () => {
const o = of('foo').pipe(first(undefined, false)); // $ExpectType Observable<string | boolean>
});

it('should default D to T with an undfined predicate', () => {
const o = of('foo').pipe(first<string>(undefined)); // $Observable<string>
});

it('should support a null predicate with no default', () => {
const o = of('foo').pipe(first(null)); // $ExpectType Observable<string>
});

it('should support a null predicate with a T default', () => {
const o = of('foo').pipe(first(null, 'bar')); // $ExpectType Observable<string>
});

it('should support a null predicate with a non-T default', () => {
const o = of('foo').pipe(first(null, false)); // $ExpectType Observable<string | boolean>
});

it('should default D to T with a null predicate', () => {
const o = of('foo').pipe(first<string>(null)); // $Observable<string>
});

it('should support a user-defined type guard with no default', () => {
const o = of('foo').pipe(first(isFooBar)); // $ExpectType Observable<"foo" | "bar">
});

it('should support a user-defined type guard with an S default', () => {
const o = of('foo').pipe(first(isFooBar, 'bar')); // $ExpectType Observable<"foo" | "bar">
});

it('should widen a user-defined type guard with a non-S default', () => {
const o = of('foo').pipe(first(isFooBar, false)); // $ExpectType Observable<string | boolean>
});

it('should support a predicate with no default', () => {
const o = of('foo').pipe(first(x => !!x)); // $ExpectType Observable<string>
});

it('should support a predicate with a T default', () => {
const o = of('foo').pipe(first(x => !!x, 'bar')); // $ExpectType Observable<string>
});

it('should support a predicate with a non-T default', () => {
const o = of('foo').pipe(first(x => !!x, false)); // $ExpectType Observable<string | boolean>
});

it('should default D to T with a predicate', () => {
const o = of('foo').pipe(first<string>(x => !!x)); // $Observable<string>
});
64 changes: 64 additions & 0 deletions spec-dtslint/operators/last-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { of } from 'rxjs';
import { last } from 'rxjs/operators';

const isFooBar = (value: string): value is 'foo' | 'bar' => /^(foo|bar)$/.test(value);

it('should support an undefined predicate with no default', () => {
const o = of('foo').pipe(last(undefined)); // $ExpectType Observable<string>
});

it('should support an undefined predicate with a T default', () => {
const o = of('foo').pipe(last(undefined, 'bar')); // $ExpectType Observable<string>
});

it('should support an undefined predicate with a non-T default', () => {
const o = of('foo').pipe(last(undefined, false)); // $ExpectType Observable<string | boolean>
});

it('should default D to T with an undfined predicate', () => {
const o = of('foo').pipe(last<string>(undefined)); // $Observable<string>
});

it('should support a null predicate with no default', () => {
const o = of('foo').pipe(last(null)); // $ExpectType Observable<string>
});

it('should support a null predicate with a T default', () => {
const o = of('foo').pipe(last(null, 'bar')); // $ExpectType Observable<string>
});

it('should support a null predicate with a non-T default', () => {
const o = of('foo').pipe(last(null, false)); // $ExpectType Observable<string | boolean>
});

it('should default D to T with a null predicate', () => {
const o = of('foo').pipe(last<string>(null)); // $Observable<string>
});

it('should support a user-defined type guard with no default', () => {
const o = of('foo').pipe(last(isFooBar)); // $ExpectType Observable<"foo" | "bar">
});

it('should support a user-defined type guard with an S default', () => {
const o = of('foo').pipe(last(isFooBar, 'bar')); // $ExpectType Observable<"foo" | "bar">
});

it('should widen a user-defined type guard with a non-S default', () => {
const o = of('foo').pipe(last(isFooBar, false)); // $ExpectType Observable<string | boolean>
});

it('should support a predicate with no default', () => {
const o = of('foo').pipe(last(x => !!x)); // $ExpectType Observable<string>
});

it('should support a predicate with a T default', () => {
const o = of('foo').pipe(last(x => !!x, 'bar')); // $ExpectType Observable<string>
});

it('should support a predicate with a non-T default', () => {
const o = of('foo').pipe(last(x => !!x, false)); // $ExpectType Observable<string | boolean>
});

it('should default D to T with a predicate', () => {
const o = of('foo').pipe(last<string>(x => !!x)); // $Observable<string>
});
26 changes: 13 additions & 13 deletions src/internal/operators/first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import { Observable } from '../Observable';
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { EmptyError } from '../util/EmptyError';
import { MonoTypeOperatorFunction, OperatorFunction } from '../../internal/types';
import { OperatorFunction } from '../../internal/types';
import { filter } from './filter';
import { take } from './take';
import { defaultIfEmpty } from './defaultIfEmpty';
import { throwIfEmpty } from './throwIfEmpty';
import { identity } from '../util/identity';

/* tslint:disable:max-line-length */
export function first<T>(
export function first<T, D = T>(
predicate?: null,
defaultValue?: T
): MonoTypeOperatorFunction<T>;
defaultValue?: D
): OperatorFunction<T, T | D>;
export function first<T, S extends T>(
predicate: (value: T, index: number, source: Observable<T>) => value is S,
defaultValue?: T
defaultValue?: S
): OperatorFunction<T, S>;
export function first<T>(
export function first<T, D = T>(
predicate: (value: T, index: number, source: Observable<T>) => boolean,
defaultValue?: T
): MonoTypeOperatorFunction<T>;
defaultValue?: D
): OperatorFunction<T, T | D>;
/* tslint:enable:max-line-length */

/**
Expand Down Expand Up @@ -72,14 +72,14 @@ export function first<T>(
* @method first
* @owner Observable
*/
export function first<T>(
predicate?: (value: T, index: number, source: Observable<T>) => boolean,
defaultValue?: T
): MonoTypeOperatorFunction<T> {
export function first<T, D>(
predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
defaultValue?: D
): OperatorFunction<T, T | D> {
const hasDefaultValue = arguments.length >= 2;
return (source: Observable<T>) => source.pipe(
predicate ? filter((v, i) => predicate(v, i, source)) : identity,
take(1),
hasDefaultValue ? defaultIfEmpty(defaultValue) : throwIfEmpty(() => new EmptyError()),
hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
);
}
26 changes: 13 additions & 13 deletions src/internal/operators/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import { Observable } from '../Observable';
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { EmptyError } from '../util/EmptyError';
import { MonoTypeOperatorFunction, OperatorFunction } from '../../internal/types';
import { OperatorFunction } from '../../internal/types';
import { filter } from './filter';
import { takeLast } from './takeLast';
import { throwIfEmpty } from './throwIfEmpty';
import { defaultIfEmpty } from './defaultIfEmpty';
import { identity } from '../util/identity';

/* tslint:disable:max-line-length */
export function last<T>(
export function last<T, D = T>(
predicate?: null,
defaultValue?: T
): MonoTypeOperatorFunction<T>;
defaultValue?: D
): OperatorFunction<T, T | D>;
export function last<T, S extends T>(
predicate: (value: T, index: number, source: Observable<T>) => value is S,
defaultValue?: T
defaultValue?: S
): OperatorFunction<T, S>;
export function last<T>(
export function last<T, D = T>(
predicate: (value: T, index: number, source: Observable<T>) => boolean,
defaultValue?: T
): MonoTypeOperatorFunction<T>;
defaultValue?: D
): OperatorFunction<T, T | D>;
/* tslint:enable:max-line-length */

/**
Expand All @@ -41,14 +41,14 @@ export function last<T>(
* from the source, or an NoSuchElementException if no such items are emitted.
* @throws - Throws if no items that match the predicate are emitted by the source Observable.
*/
export function last<T>(
predicate?: (value: T, index: number, source: Observable<T>) => boolean,
defaultValue?: T
): MonoTypeOperatorFunction<T> {
export function last<T, D>(
predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
defaultValue?: D
): OperatorFunction<T, T | D> {
const hasDefaultValue = arguments.length >= 2;
return (source: Observable<T>) => source.pipe(
predicate ? filter((v, i) => predicate(v, i, source)) : identity,
takeLast(1),
hasDefaultValue ? defaultIfEmpty(defaultValue) : throwIfEmpty(() => new EmptyError()),
hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
);
}