Skip to content
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

Improved handling of structurally identical classes #19671

Merged
merged 9 commits into from
Nov 3, 2017

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Nov 1, 2017

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.

@mhegazy mhegazy added the Breaking Change Would introduce errors in existing code label Nov 2, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 2, 2017
@ahejlsberg ahejlsberg merged commit add8b49 into master Nov 3, 2017
@ahejlsberg ahejlsberg deleted the nominalInstanceof branch November 3, 2017 00:37
@akarzazi
Copy link

This may fix #11664 too : )

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code
Projects
None yet
3 participants