Skip to content
Permalink
Browse files

fix(pluck): key union type strictness (#4585)

  • Loading branch information...
imcotton authored and benlesh committed May 16, 2019
1 parent fc9186c commit bd5ec2d710723dda64f75d2dae0f7b53244f8dfd
@@ -28,5 +28,6 @@ import { pluck as higherOrder } from 'rxjs/operators';
* @owner Observable
*/
export function pluck<T, R>(this: Observable<T>, ...properties: string[]): Observable<R> {
// @ts-ignore
return higherOrder(...properties)(this) as Observable<R>;
}
@@ -29,12 +29,12 @@ it('should support nested object of more than 6 layer depth', () => {
const a = of({ a: { b: { c: { d: { e: { f: { name: 'abc' } } } } } } }).pipe(pluck('a', 'b', 'c', 'd', 'e', 'f', 'name')); // $ExpectType Observable<{}>
});

it('should infer empty interface for non-existance key', () => {
const a = of({ name: 'abc' }).pipe(pluck('xyz')); // $ExpectType Observable<{}>
it('should accept existing keys only', () => {
const a = of({ name: 'abc' }).pipe(pluck('xyz')); // $ExpectError
});

it('should infer empty interface for empty parameter', () => {
const a = of({ name: 'abc' }).pipe(pluck()); // $ExpectType Observable<{}>
it('should not accept empty parameter', () => {
const a = of({ name: 'abc' }).pipe(pluck()); // $ExpectError
});

it('should accept string only', () => {
@@ -82,13 +82,15 @@ describe('pluck operator', () => {
const expected = '--r-x--y-z---w-|';
const values: { [key: string]: number | undefined } = {r: 1, x: undefined, y: undefined, z: undefined, w: 5};

// @ts-ignore
const r = a.pipe(pluck('a', 'b', 'c'));
expectObservable(r).toBe(expected, values);
expectSubscriptions(a.subscriptions).toBe(asubs);
});

it('should throw an error if not property is passed', () => {
expect(() => {
// @ts-ignore
of({prop: 1}, {prop: 2}).pipe(pluck());
}).to.throw(Error, 'list of properties cannot be empty.');
});
@@ -98,6 +100,7 @@ describe('pluck operator', () => {
const asubs = '(^!)';
const expected = '#';

// @ts-ignore
const r = a.pipe(pluck('whatever'));
expectObservable(r).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
@@ -120,6 +123,7 @@ describe('pluck operator', () => {

const invoked = 0;
const r = a.pipe(
// @ts-ignore
pluck('whatever'),
tap(null, null, () => {
expect(invoked).to.equal(0);
@@ -169,7 +173,7 @@ describe('pluck operator', () => {

const r = a.pipe(
mergeMap((x: { prop: string }) => of(x)),
pluck<{ prop: string }, string>('prop'),
pluck('prop'),
mergeMap((x: string) => of(x))
);

@@ -9,7 +9,7 @@ export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends
export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(k1: K1, k2: K2, k3: K3, k4: K4): OperatorFunction<T, T[K1][K2][K3][K4]>;
export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction<T, T[K1][K2][K3][K4][K5]>;
export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction<T, T[K1][K2][K3][K4][K5][K6]>;
export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R>;
export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5], R>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction<T, R>;
/* tslint:enable:max-line-length */

/**

0 comments on commit bd5ec2d

Please sign in to comment.
You can’t perform that action at this time.