GeezerVison is showing github.com — Improved handling of structurally identical classes by ahejlsberg · Pull Request #19671 · microsoft/TypeScript
Conversation
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|