Skip to content

Commit e0c5b7c

Browse files
Lonelindbenlesh
authored andcommittedJan 20, 2020
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: #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
1 parent 56e7ceb commit e0c5b7c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed
 

‎spec-dtslint/operators/pluck-spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { of } from 'rxjs';
1+
import { of, Observable } from 'rxjs';
22
import { pluck } from 'rxjs/operators';
33

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

44+
it('should not infer type from the variable if key doesn\'t exist', () => {
45+
const a: Observable<number> = of({ name: 'abc' }).pipe(pluck('xyz')); // $ExpectError
46+
});
47+
4448
it('should accept a spread of arguments', () => {
4549
const obj = {
4650
foo: {

‎src/internal/operators/pluck.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends
99
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]>;
1010
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]>;
1111
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]>;
12-
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>;
13-
export function pluck<T, R= unknown>(...properties: string[]): OperatorFunction<T, R>;
12+
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>;
13+
export function pluck<T>(...properties: string[]): OperatorFunction<T, unknown>;
1414
/* tslint:enable:max-line-length */
1515

1616
/**

0 commit comments

Comments
 (0)
Please sign in to comment.