Improve type inference of `pluck` operator
See original GitHub issueRxJS version: 5.4.3
Code to reproduce:
const obs = Observable.of([{ a: { b: 1 } }]);
const a = obs.pluck('a');
Expected behavior:
a
be of type Observable<{b: 1}>
.
Actual behavior:
a
is of type Observable<{}>
Additional information:
While I realise that I can provide the type of the return value to pluck
, doing so doesn’t give me any type safety. Instead, this could be inferred, at least for an arbitrary level of nesting. Anyone who goes beyond this level of nesting wouldn’t be able to use type inference, very similarly to the current state of things.
type Pluck<T, K extends keyof T> = T[K];
export function pluck<
T,
P1 extends keyof T
>(
this: Observable<T>,
property1: P1,
): Observable<Pluck<T, P1>>;
export function pluck<
T,
P1 extends keyof T,
P2 extends keyof T[P1]
>(
this: Observable<T>,
property1: P1,
property2: P2,
): Observable<Pluck<Pluck<T, P1>, P2>>;
export function pluck<
T,
P1 extends keyof T,
P2 extends keyof T[P1],
P3 extends keyof T[P1][P2]
>(
this: Observable<T>,
property1: P1,
property2: P2,
property3: P3,
): Observable<Pluck<Pluck<Pluck<T, P1>, P2>, P3>>;
export function pluck<
T,
P1 extends keyof T,
P2 extends keyof T[P1],
P3 extends keyof T[P1][P2],
P4 extends keyof T[P1][P2][P3]
>(
this: Observable<T>,
property1: P1,
property2: P2,
property3: P3,
property4: P4,
): Observable<Pluck<Pluck<Pluck<Pluck<T, P1>, P2>, P3>, P4>>;
export function pluck<T>(
this: Observable<T>,
...properties: string[]
): Observable<any> {
return higherOrder(...properties)(this);
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >What's the difference between map and pluck in RxJS?
map ), while Pluck is used to select/pick a property to emit (without having to emit properties that we don't care for, hence...
Read more >pluck - RxJS
OperatorFunction <T, R> : A function that returns an Observable of property values from the source values. Descriptionlink. Like map , but meant...
Read more >Type System - MuleSoft Documentation
Any type accepts all possible values. · Nothing type accepts no value, but it can be assigned to all the types. This type...
Read more >RxJS 7 in-depth - JavaScript in Plain English
This is super powerful for type inference as it allows to use custom type guards like the ... but also to combine combineLatest...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is this ready to be reopened?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.