Skip to content

Commit

Permalink
fix(pluck): key union type strictness (#4585)
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton authored and benlesh committed May 16, 2019
1 parent fc9186c commit bd5ec2d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions compat/operator/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
8 changes: 4 additions & 4 deletions spec-dtslint/operators/pluck-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion spec/operators/pluck-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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))
);

Expand Down
2 changes: 1 addition & 1 deletion src/internal/operators/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
Expand Down

0 comments on commit bd5ec2d

Please sign in to comment.