Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(partition): add exclusion when using type guard #6168

Merged
merged 1 commit into from
Mar 25, 2021

Conversation

Boudewijn26
Copy link
Contributor

Description:
Add exclusion to partition when using a type guard.

Previously support for a user defined type guard was added to partition. This meant that Typescript could understand the first item in the resulting tuple to match a certain type. What was missing was Typescript understanding the second item in the tuple to not be of that type. As an example:

type AMessage = { a: number };
type BMessage = { b: number };
type Message = AMessage | BMessage;
const aMessage: AMessage = { a: 1 };
const bMessage: BMessage = { b: 2 };
const message$: Observable<Message> = from([aMessage, bMessage]);

function isAMessage(msg: Message): is AMessage { 
  return msg.a;
}
const [aMessage$, bMessage$] = partition(message$, isAMessage);
bMessage$.subscribe((msg) => console.log(msg.b)); // no longer fails with this change

Without this change Typescript would consider bMessage$ to be the same type as message$: Observable<AMessage | BMessage>. Whereas we are sure bMessage$ only emits BMessage, because all AMessage will be put in aMessage$.

@cartant cartant changed the title docs(partition): add exclusion when using type guard fix(partition): add exclusion when using type guard Mar 23, 2021
src/internal/observable/partition.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@Boudewijn26
Copy link
Contributor Author

Thank you for the quick response, happy to help!

@benlesh benlesh merged commit 8e5f90a into ReactiveX:master Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants