From e0c5b7c790bb9d99fa8bee26c805b5e70c1e456b Mon Sep 17 00:00:00 2001 From: Alexander Kuzmin Date: Mon, 20 Jan 2020 23:29:21 +0300 Subject: [PATCH] fix(pluck): fix pluck's catch-all signature for better type safety (#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: https://github.com/ReactiveX/rxjs/issues/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 --- spec-dtslint/operators/pluck-spec.ts | 6 +++++- src/internal/operators/pluck.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/spec-dtslint/operators/pluck-spec.ts b/spec-dtslint/operators/pluck-spec.ts index d257f26d9b..a4ef259c09 100644 --- a/spec-dtslint/operators/pluck-spec.ts +++ b/spec-dtslint/operators/pluck-spec.ts @@ -1,4 +1,4 @@ -import { of } from 'rxjs'; +import { of, Observable } from 'rxjs'; import { pluck } from 'rxjs/operators'; it('should infer correctly', () => { @@ -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 = of({ name: 'abc' }).pipe(pluck('xyz')); // $ExpectError +}); + it('should accept a spread of arguments', () => { const obj = { foo: { diff --git a/src/internal/operators/pluck.ts b/src/internal/operators/pluck.ts index 2cb5fb0b51..eef435184f 100644 --- a/src/internal/operators/pluck.ts +++ b/src/internal/operators/pluck.ts @@ -9,8 +9,8 @@ export function pluck(k1: K1, k2: K2, k3: K3, k4: K4): OperatorFunction; export function pluck(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction; export function pluck(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction; -export function pluck(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction; -export function pluck(...properties: string[]): OperatorFunction; +export function pluck(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction; +export function pluck(...properties: string[]): OperatorFunction; /* tslint:enable:max-line-length */ /**