Skip to content

Commit bd5ec2d

Browse files
imcottonbenlesh
authored andcommitted
fix(pluck): key union type strictness (#4585)
1 parent fc9186c commit bd5ec2d

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

compat/operator/pluck.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ import { pluck as higherOrder } from 'rxjs/operators';
2828
* @owner Observable
2929
*/
3030
export function pluck<T, R>(this: Observable<T>, ...properties: string[]): Observable<R> {
31+
// @ts-ignore
3132
return higherOrder(...properties)(this) as Observable<R>;
3233
}

spec-dtslint/operators/pluck-spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ it('should support nested object of more than 6 layer depth', () => {
2929
const a = of({ a: { b: { c: { d: { e: { f: { name: 'abc' } } } } } } }).pipe(pluck('a', 'b', 'c', 'd', 'e', 'f', 'name')); // $ExpectType Observable<{}>
3030
});
3131

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

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

4040
it('should accept string only', () => {

spec/operators/pluck-spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ describe('pluck operator', () => {
8282
const expected = '--r-x--y-z---w-|';
8383
const values: { [key: string]: number | undefined } = {r: 1, x: undefined, y: undefined, z: undefined, w: 5};
8484

85+
// @ts-ignore
8586
const r = a.pipe(pluck('a', 'b', 'c'));
8687
expectObservable(r).toBe(expected, values);
8788
expectSubscriptions(a.subscriptions).toBe(asubs);
8889
});
8990

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

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

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

170174
const r = a.pipe(
171175
mergeMap((x: { prop: string }) => of(x)),
172-
pluck<{ prop: string }, string>('prop'),
176+
pluck('prop'),
173177
mergeMap((x: string) => of(x))
174178
);
175179

src/internal/operators/pluck.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ 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, R>(...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], R>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction<T, R>;
1313
/* tslint:enable:max-line-length */
1414

1515
/**

0 commit comments

Comments
 (0)