GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Union subtype reduction by ahejlsberg · Pull Request #4537 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Aug 28, 2015

Copy link
Copy Markdown
Member

This PR rolls back some of the changes related to union types introduced in #3823. Specifically, we now again use subtype reduction as the primary means of removing redundant constituent types in union types. This improves backwards compatibility without impacting the strict object literal assignment checking that was the primary purpose of #3823.

It turns out that subtype reduction isn't at odds with strict object literal assignment checks as long as we are a bit more selective about where we perform the reduction. Specifically, because fresh object literals are subtypes only of object types with an exact matching set of properties (the fresh object literals can have neither too many nor too few properties), we get pretty much the same behavior from subtype checks as we did from the new deduplication algorithm in #3823.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Anders Hejlsberg (ahejlsberg) added 3 commits August 28, 2015 14:50
@ahejlsberg
Restoring union type subtype reduction
d8d7fb9
@ahejlsberg
Fixing fourslash tests
00e7c46
@ahejlsberg
Accepting new baselines
49bb2bb
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Aug 28, 2015
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Aug 28, 2015

Copy link
Copy Markdown
Member Author

Jason Freeman (@JsonFreeman) Thought this might interest you!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DanielRosenwasser
DanielRosenwasser reviewed Aug 28, 2015
View reviewed changes
Comment thread src/compiler/checker.ts

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Aug 28, 2015

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Perhaps isExpectedProperty would better describe this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JsonFreeman

Jason Freeman (JsonFreeman) commented Aug 28, 2015

Copy link
Copy Markdown
Contributor

Whoa, interesting! That's true about fresh object literals failing the subtype test for subtype reduction. I have two general questions:

  1. Is there any concrete benefit to having subtype reduction (given the new way of computing signatures of union types), or is it just for simplicity - to avoid having an extra type relation?
  2. What is the difference between this incarnation of subtype reduction and the way it used to be? Presumably this fact about fresh object literals not getting thrown away would have applied before as well.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Aug 28, 2015

Copy link
Copy Markdown
Contributor

👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DanielRosenwasser
DanielRosenwasser reviewed Aug 29, 2015
View reviewed changes
Comment thread src/compiler/checker.ts Outdated

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Aug 29, 2015

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Use let

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Aug 29, 2015

Copy link
Copy Markdown
Member Author

Is there any concrete benefit to having subtype reduction (given the new way of computing signatures of union types), or is it just for simplicity - to avoid having an extra type relation?

It's mostly a backwards compatibility thing. We've seen several examples of real world code that was broken by the new behavior. The breaks all had to do with call signatures disappearing because a union type didn't get reduced to a single supertype. Of course it doesn't hurt that this new (or perhaps I should say old) way of doing things is also simpler in that there isn't a new kind of type relationship we have to document and reason about.

What is the difference between this incarnation of subtype reduction and the way it used to be? Presumably this fact about fresh object literals not getting thrown away would have applied before as well.

Consider this example:

var a = [{ x: 1 }, { x: 2, y: 2 }];  // ({ x: number } | { x: number, y: number })[]

Because the elements have fresh object literal types they aren't subtypes of each other, so the element type of the array literal is a union type. As we widen the type of the array literal we make sure _not_ to perform subtype reduction again as that would collapse the union when the object literal types lose their freshness. So, the full type survives as the type of a and the array literal is assignable to a. That previously wasn't the case (at one point in the evolution of #3823 we were in a state where the above was an error), and that's really the big difference.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg
Addressing CR feedback
603eb86
Mohamed Hegazy (mhegazy) added a commit that referenced this pull request Aug 29, 2015
@mhegazy
Merge pull request #4537 from Microsoft/unionSubtypeReduction
7f6608b
Union subtype reduction
@mhegazy
Mohamed Hegazy (mhegazy) merged commit 7f6608b into master Aug 29, 2015
@mhegazy
Mohamed Hegazy (mhegazy) deleted the unionSubtypeReduction branch August 29, 2015 00:48
@JsonFreeman

Jason Freeman (JsonFreeman) commented Aug 29, 2015

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg) Got it. Thanks for the explanation! So the difference is that noSubtypeReduction is true for the call to getUnionType in getWidenedType? That would seem to be the trap where subtype reduction is dangerous.

The scheme of aggregating the signatures of a union type is remaining as is, correct? I think it's still good for union types that didn't collapse under subtype reduction.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jcalz Joe Calzaretta (jcalz) mentioned this pull request Jun 27, 2017
typeToString fails to reduce subtypes #16582
Closed
@microsoft 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.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@ahejlsberg @JsonFreeman @mhegazy @DanielRosenwasser @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.