Skip to content

Commit

Permalink
fix(pluck): fix pluck's catch-all signature for better type safety (#…
Browse files Browse the repository at this point in the history
…5192)

* fix(pluck): fix pluck's catch-all signature for better type safety

Change pluck's catch-all signature that is causing troubles when the
passed string is not a key in the argument object.

Issue: #5188

* fix(pluck): fix signature and tests

Correct the signtaure to be accepted by tests and to return unknown in
general

* fix(pluck): add test case to check type inference
  • Loading branch information
Lonelind authored and benlesh committed Jan 20, 2020
1 parent 56e7ceb commit e0c5b7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion spec-dtslint/operators/pluck-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { of } from 'rxjs';
import { of, Observable } from 'rxjs';
import { pluck } from 'rxjs/operators';

it('should infer correctly', () => {
Expand Down Expand Up @@ -41,6 +41,10 @@ it('should accept string only', () => {
const a = of({ name: 'abc' }).pipe(pluck(1)); // $ExpectError
});

it('should not infer type from the variable if key doesn\'t exist', () => {
const a: Observable<number> = of({ name: 'abc' }).pipe(pluck('xyz')); // $ExpectError
});

it('should accept a spread of arguments', () => {
const obj = {
foo: {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/operators/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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, 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>;
export function pluck<T, R= unknown>(...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]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction<T, unknown>;
export function pluck<T>(...properties: string[]): OperatorFunction<T, unknown>;
/* tslint:enable:max-line-length */

/**
Expand Down

0 comments on commit e0c5b7c

Please sign in to comment.