Skip to content

Use "best choice type" for || and ?: operators #10069

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 4 commits into from
Aug 1, 2016
Merged

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Aug 1, 2016

This PR introduces a best choice type for the || and ?: operators. Given two types T1 and T2, the best choice type of T1 and T2 is determined as follows:

  • If T2 is assignable to T1 and T1 is not assignable to T2, the best choice type is T1.
  • Otherwise, if T1 is assignable to T2 and T2 is not assignable to T1, the best choice type is T2.
  • Otherwise, if T2 is a subtype of T1, the best choice type is T1.
  • Otherwise, if T1 is a subtype of T2, the best choice type is T2.
  • Otherwise, the best choice type is the union type T1 | T2.

The first two steps are the ones introduced by this PR. They make the operators slightly less likely to produce a union type than they were before. In particular, if T2 is a subtype of T1 except for optional properties in T1 that are missing in T2, we will now pick T1 instead of producing the union type T1 | T2.

Fixes #10041.

@RyanCavanaugh
Copy link
Member

👍 makes sense

@ahejlsberg ahejlsberg merged commit 1435fb1 into master Aug 1, 2016
@weswigham weswigham deleted the bestChoiceType branch August 11, 2016 01:43
@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