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 — Improved handling of structurally identical classes by ahejlsberg · Pull Request #19671 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Nov 1, 2017 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

With this PR we improve the handling of structurally identical classes in union types and instanceof expressions:

  • Structurally identical, but distinct, class types are now preserved in union types (instead of eliminating all but one).
  • Union type subtype reduction only removes a class type if it is a subclass of and derives from another class type in the union.
  • Type checking of the instanceof operator is now based on whether the type of the left operand derives from the type indicated by the right operand (as opposed to a structural subtype check).

This means that union types and instanceof properly distinguish between structurally identical classes. For example:

class A {}
class B extends A {}
class C extends A {}
class D extends A { c: string }
class E extends D {}

let x1 = !true ? new A() : new B();  // A
let x2 = !true ? new B() : new C();  // B | C (previously B)
let x3 = !true ? new C() : new D();  // C | D (previously C)

let a1 = [new A(), new B(), new C(), new D(), new E()];  // A[]
let a2 = [new B(), new C(), new D(), new E()];  // (B | C | D)[] (previously B[])

function f1(x: B | C | D) {
    if (x instanceof B) {
        x;  // B (previously B | D)
    }
    else if (x instanceof C) {
        x;  // C
    }
    else {
        x;  // D (previously never)
    }
}

The comments show the places where we previously got it wrong (or, I should say, less right).

Note that this PR does not change the fact that structurally identical types are subtypes and assignment compatible. The changes are entirely confined to the instanceof operator and reduction of union types.

The PR is technically a breaking change because we now preserve unions of structurally identical class types where we would previously reduce the union to one of the class types. Any errors resulting from this should be fixable with a type assertion or a type annotation. (There are no new errors compiling DefinitelyTyped, but a couple in the real world code suites.)

Fixes #10934.
Fixes #11965.
Fixes #14426.
Fixes #15585.
Fixes #15615.
Fixes #17612.
Fixes #18276.

Sorry, something went wrong.

Uh oh!

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

trotyl, drewnoakes, akarzazi, bradleyayers, vmasek, sbichenko, HerringtonDarkholme, elektronik2k5, and LastDragon-ru reacted with thumbs up emoji SlurpTheo, trotyl, and xnnkmd reacted with hooray emoji alexrock, RyanCavanaugh, topaxi, jbrownson, devmobasa, kitsonk, trotyl, and olaf89 reacted with heart emoji
All reactions
  • 👍 9 reactions
  • 🎉 3 reactions
  • ❤️ 8 reactions
Anders Hejlsberg (ahejlsberg) added 9 commits October 30, 2017 12:36
@ahejlsberg
Use nominal check in isTypeInstanceOf
a01df0f
@ahejlsberg
Accept new baselines
d669771
@ahejlsberg
Use nominal checks in union type subtype reduction
3d89837
@ahejlsberg
Accept new baselines
923e7a0
@ahejlsberg
Nix getBestChoiceType, [] subtyping, nominal union reduction for classes
25af351
@ahejlsberg
Add type annotation
7f35c8f
@ahejlsberg
Accept new baselines
412f373
@ahejlsberg
Merge branch 'master' into nominalInstanceof
2a30aaf
@ahejlsberg
Properly handle Object and Function types
81326ac
@ahejlsberg
Anders Hejlsberg (ahejlsberg) requested review from Daniel Rosenwasser (DanielRosenwasser), Ryan Cavanaugh (RyanCavanaugh) and Mohamed Hegazy (mhegazy) November 1, 2017 22:51
mhegazy
Mohamed Hegazy (mhegazy) approved these changes Nov 2, 2017
View reviewed changes
@mhegazy Mohamed Hegazy (mhegazy) added the Breaking Change Would introduce errors in existing code label Nov 2, 2017
@mhegazy Mohamed Hegazy (mhegazy) added this to the TypeScript 2.7 milestone Nov 2, 2017
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit add8b49 into master Nov 3, 2017
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the nominalInstanceof branch November 3, 2017 00:37
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Nov 3, 2017
Design Meeting Notes, 11/3/2017 #19722
Closed
@drewnoakes Drew Noakes (drewnoakes) mentioned this pull request Nov 17, 2017
instanceof typeguard fails if classes have similar structure #7271
Closed
@akarzazi

Adel KARZAZI (akarzazi) commented Nov 17, 2017

Copy link
Copy Markdown

This may fix #11664 too : )

All reactions

Sorry, something went wrong.

Uh oh!

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

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Nov 17, 2017
Wrong type narrowing when types are compatibles #11664
Closed
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Nov 19, 2017
instanceof type guard missing deduction #20138
Closed
@weswigham Wesley Wigham (weswigham) mentioned this pull request Dec 29, 2017
Destructuring assignment makes any types when it skips some elements (regression) #20872
Closed
@HerringtonDarkholme Herrington Darkholme (HerringtonDarkholme) mentioned this pull request Jan 2, 2018
Incorrect 'never' type inference in TypeScript 2.6 #20952
Closed
@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Jan 31, 2018
regression with private class members and loosing visibility #21516
Closed
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Awaiting requested review from DanielRosenwasser
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Awaiting requested review from RyanCavanaugh
1 more reviewer
@mhegazy Mohamed Hegazy (mhegazy) mhegazy approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

Breaking Change Would introduce errors in existing code

Projects

None yet

Milestone

TypeScript 2.7

Development

Successfully merging this pull request may close these issues.

this subclass inference problem instanceof / structural typing issue 'instanceof' narrowing works awkwardly with empty object types & primitives instanceof type inference broken in an "if -> else if -> else if" chain never incorrectly inferred with type guard
instanceof typeguard fails with derived classes that have unsound overrides Strange warning with 'instanceof'

Uh oh!

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

3 participants

@ahejlsberg @akarzazi @mhegazy
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.