Skip to content

Custom operator affects prevoius operator. #6484

Answered by raymonddavis
Totati asked this question in Q&A
Discussion options

You must be logged in to vote

The typing is incorrect on your custom operators.

export function isBlack(): OperatorFunction<Fruit | Fish, boolean> {
  return source => source.pipe(map(v => v.color === 'black'));
}

This implies that your operator will return Fruit | Fish.
What i think you want to do is this

export function isBlack<T extends Fruit | Fish>(): OperatorFunction<T, boolean> {
  return source => source.pipe(map(v => v.color === 'black'));
}

This allows Fruit | Fish to be passed in and whatever one is passed is also returned.

Your previous example will allow anything to be passed in and hard setting the return to a "new" type Fruit | Fish

Updated typing
Stackblitz example

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Totati
Comment options

Answer selected by Totati
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants