question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve type inference of `pluck` operator

See original GitHub issue

RxJS 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aboytoncommented, Mar 22, 2018

Is this ready to be reopened?

0reactions
lock[bot]commented, Jun 5, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found