Skip to content

How should empty arrays and union types work together in 1.6? #4499

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

Closed
osdm opened this issue Aug 27, 2015 · 2 comments
Closed

How should empty arrays and union types work together in 1.6? #4499

osdm opened this issue Aug 27, 2015 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@osdm
Copy link

osdm commented Aug 27, 2015

var p = new Array<string>();
var q = true ? p : [];
q[0]

My reasoning follows, please correct me if I'm wrong.
According to 1.5 spec, type of q[0] was calculated in the following way:

  1. Take union type of Array<string> and empty array (Array<Undefined>).
  2. Reduce that union type, which gives us Array<string>, because string is a supertype of undefined.
  3. Widen that type (changes nothing).
  4. Type of q[0] is string.

According to 1.6 spec, type of q[0] should be calculated in the following way:

  1. Take union type of Array<string> and empty array (Array<Undefined>).
  2. No reduce is happening.
  3. Widen that type, resulting in (Array<string> | Array<any>).
  4. Type of q[0] is (string | any).

But that would break "no implicit any". Where did I make a mistake?

@DanielRosenwasser
Copy link
Member

@ahejlsberg, can you check into this?

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Aug 27, 2015
@ahejlsberg
Copy link
Member

@osdm You're right, we no longer do subtype reduction in union types, however we still do deduplication as discussed in #4115. This has yet to make it into the spec. In the example above, Array<Undefined> is considered a duplicate of Array<string> so the type of the ?: operation simply becomes Array<string>.

@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants