Skip to content

Fix discriminated unions with primtive types #10296

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

Merged
merged 8 commits into from
Aug 12, 2016

Conversation

ahejlsberg
Copy link
Member

Fixes #10257.

return type.flags & TypeFlags.Union ?
getUnionType(filter((<UnionType>type).types, f)) :
f(type) ? type : neverType;
if (!(type.flags & TypeFlags.Union)) {
Copy link
Member

Choose a reason for hiding this comment

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

The rewrite here makes sense (I'm assuming this is a perf thing?) but deserves a comment as to why it isn't using the existing filter code so that a maintainer doesn't just undo this change later.

Copy link
Member Author

@ahejlsberg ahejlsberg Aug 11, 2016

Choose a reason for hiding this comment

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

Yes, it is an optimization to avoid doing any work if the filter function returns true for all constituents. But now that you mention it, it would make more sense to push the optimization into the filter function itself. Similar to how concatenate doesn't allocate a new array unless it has to, so could filter just return the argument array itself when the predicate is true for all elements.

@RyanCavanaugh
Copy link
Member

👍

@ahejlsberg ahejlsberg merged commit 19d89c4 into master Aug 12, 2016
@ahejlsberg ahejlsberg deleted the fixDiscriminantWithPrimtive branch August 12, 2016 20:18
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants