-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Elaborate on types in unions with the most overlap in properties #27087
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ab8bd8
Try to find object/intersection types with the most overlap when fail…
DanielRosenwasser 76e721f
Added tests.
DanielRosenwasser 91af958
Accepted baselines.
DanielRosenwasser 6ae288f
Merge remote-tracking branch 'origin/master' into overlappyTypes
DanielRosenwasser b7da98b
Use intersection of member names to find largest overlap.
DanielRosenwasser 5a4378e
Accepted baselines.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11472,7 +11472,8 @@ namespace ts { | |
findMatchingDiscriminantType(source, target) || | ||
findMatchingTypeReferenceOrTypeAliasReference(source, target) || | ||
findBestTypeForObjectLiteral(source, target) || | ||
findBestTypeForInvokable(source, target); | ||
findBestTypeForInvokable(source, target) || | ||
findMostOverlappyType(source, target); | ||
|
||
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true); | ||
} | ||
|
@@ -11512,6 +11513,32 @@ namespace ts { | |
} | ||
} | ||
|
||
function findMostOverlappyType(source: Type, unionTarget: UnionOrIntersectionType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I think this is a slightly cleaner implementation that should also handle generics better: function findMostOverlappyType(source: Type, unionTarget: UnionOrIntersectionType) {
let bestMatch = unionTarget.types[0];
let matchingCount = 0;
for (const target of unionTarget.types) {
const overlap = getIntersectionType([getIndexType(source), getIndexType(target)]);
if (overlap.kind & TypeFlags.Index) {
// perfect overlap of keys
bestMatch = target;
matchingCount = Infinity;
}
else if (overlap.flags & TypeFlags.Union) {
// Some subset overlap
const len = length((overlap as UnionType).types);
if (len >= matchingCount) {
bestMatch = target;
matchingCount = len;
}
}
else if (matchingCount === 0) {
bestMatch = target;
}
}
return bestMatch;
} Given something like |
||
if (!(source.flags & (TypeFlags.Object | TypeFlags.Intersection))) { | ||
return undefined; | ||
} | ||
const sourceProperties = getPropertiesOfType(source); | ||
let bestType; | ||
let count = -1; | ||
for (const target of unionTarget.types) { | ||
if (!(target.flags & (TypeFlags.Object | TypeFlags.Intersection))) { | ||
continue; | ||
} | ||
|
||
let currentCount = 0; | ||
for (const prop of sourceProperties) { | ||
if (getPropertyOfType(target, prop.escapedName)) { | ||
currentCount++; | ||
} | ||
} | ||
if (currentCount >= count) { | ||
count = currentCount; | ||
bestType = target; | ||
} | ||
} | ||
return bestType; | ||
} | ||
|
||
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly | ||
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) { | ||
let match: Type | undefined; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(18,3): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'. | ||
Type '{ a: string; b: string; }' is not assignable to type 'Foo'. | ||
Types of property 'b' are incompatible. | ||
Type 'string' is not assignable to type 'number'. | ||
tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(19,3): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'. | ||
Type '{ a: string; b: string; }' is not assignable to type 'Foo'. | ||
Types of property 'b' are incompatible. | ||
Type 'string' is not assignable to type 'number'. | ||
tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(24,5): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'. | ||
Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'. | ||
|
||
|
||
==== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts (3 errors) ==== | ||
interface Foo { | ||
a: string; | ||
b: number; | ||
}; | ||
|
||
interface Bar { | ||
b: string; | ||
} | ||
|
||
interface Other { | ||
totallyUnrelatedProperty: number; | ||
} | ||
|
||
export let x = { a: '', b: '' }; | ||
|
||
declare function f(x: Foo | Other): any; | ||
|
||
f(x); | ||
~ | ||
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'. | ||
!!! error TS2345: Type '{ a: string; b: string; }' is not assignable to type 'Foo'. | ||
!!! error TS2345: Types of property 'b' are incompatible. | ||
!!! error TS2345: Type 'string' is not assignable to type 'number'. | ||
f({ a: '', b: '' }) | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'. | ||
!!! error TS2345: Type '{ a: string; b: string; }' is not assignable to type 'Foo'. | ||
!!! error TS2345: Types of property 'b' are incompatible. | ||
!!! error TS2345: Type 'string' is not assignable to type 'number'. | ||
|
||
declare function g(x: Bar | Other): any; | ||
|
||
g(x); | ||
g({ a: '', b: '' }) | ||
~~~~~ | ||
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'. | ||
!!! error TS2345: Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'. | ||
|
||
declare function h(x: Foo | Bar | Other): any; | ||
|
||
h(x); | ||
h({ a: '', b: '' }) | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//// [errorsOnUnionsOfOverlappingObjects01.ts] | ||
interface Foo { | ||
a: string; | ||
b: number; | ||
}; | ||
|
||
interface Bar { | ||
b: string; | ||
} | ||
|
||
interface Other { | ||
totallyUnrelatedProperty: number; | ||
} | ||
|
||
export let x = { a: '', b: '' }; | ||
|
||
declare function f(x: Foo | Other): any; | ||
|
||
f(x); | ||
f({ a: '', b: '' }) | ||
|
||
declare function g(x: Bar | Other): any; | ||
|
||
g(x); | ||
g({ a: '', b: '' }) | ||
|
||
declare function h(x: Foo | Bar | Other): any; | ||
|
||
h(x); | ||
h({ a: '', b: '' }) | ||
|
||
|
||
//// [errorsOnUnionsOfOverlappingObjects01.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
; | ||
exports.x = { a: '', b: '' }; | ||
f(exports.x); | ||
f({ a: '', b: '' }); | ||
g(exports.x); | ||
g({ a: '', b: '' }); | ||
h(exports.x); | ||
h({ a: '', b: '' }); |
77 changes: 77 additions & 0 deletions
77
tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
=== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts === | ||
interface Foo { | ||
>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0)) | ||
|
||
a: string; | ||
>a : Symbol(Foo.a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 15)) | ||
|
||
b: number; | ||
>b : Symbol(Foo.b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 1, 14)) | ||
|
||
}; | ||
|
||
interface Bar { | ||
>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2)) | ||
|
||
b: string; | ||
>b : Symbol(Bar.b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 5, 15)) | ||
} | ||
|
||
interface Other { | ||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1)) | ||
|
||
totallyUnrelatedProperty: number; | ||
>totallyUnrelatedProperty : Symbol(Other.totallyUnrelatedProperty, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 9, 17)) | ||
} | ||
|
||
export let x = { a: '', b: '' }; | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10)) | ||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 16)) | ||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 23)) | ||
|
||
declare function f(x: Foo | Other): any; | ||
>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32)) | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 15, 19)) | ||
>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0)) | ||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1)) | ||
|
||
f(x); | ||
>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32)) | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10)) | ||
|
||
f({ a: '', b: '' }) | ||
>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32)) | ||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 3)) | ||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 10)) | ||
|
||
declare function g(x: Bar | Other): any; | ||
>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19)) | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 20, 19)) | ||
>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2)) | ||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1)) | ||
|
||
g(x); | ||
>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19)) | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10)) | ||
|
||
g({ a: '', b: '' }) | ||
>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19)) | ||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 3)) | ||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 10)) | ||
|
||
declare function h(x: Foo | Bar | Other): any; | ||
>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19)) | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 25, 19)) | ||
>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0)) | ||
>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2)) | ||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1)) | ||
|
||
h(x); | ||
>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19)) | ||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10)) | ||
|
||
h({ a: '', b: '' }) | ||
>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19)) | ||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 3)) | ||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 10)) | ||
|
82 changes: 82 additions & 0 deletions
82
tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
=== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts === | ||
interface Foo { | ||
a: string; | ||
>a : string | ||
|
||
b: number; | ||
>b : number | ||
|
||
}; | ||
|
||
interface Bar { | ||
b: string; | ||
>b : string | ||
} | ||
|
||
interface Other { | ||
totallyUnrelatedProperty: number; | ||
>totallyUnrelatedProperty : number | ||
} | ||
|
||
export let x = { a: '', b: '' }; | ||
>x : { a: string; b: string; } | ||
>{ a: '', b: '' } : { a: string; b: string; } | ||
>a : string | ||
>'' : "" | ||
>b : string | ||
>'' : "" | ||
|
||
declare function f(x: Foo | Other): any; | ||
>f : (x: Foo | Other) => any | ||
>x : Foo | Other | ||
|
||
f(x); | ||
>f(x) : any | ||
>f : (x: Foo | Other) => any | ||
>x : { a: string; b: string; } | ||
|
||
f({ a: '', b: '' }) | ||
>f({ a: '', b: '' }) : any | ||
>f : (x: Foo | Other) => any | ||
>{ a: '', b: '' } : { a: string; b: string; } | ||
>a : string | ||
>'' : "" | ||
>b : string | ||
>'' : "" | ||
|
||
declare function g(x: Bar | Other): any; | ||
>g : (x: Bar | Other) => any | ||
>x : Bar | Other | ||
|
||
g(x); | ||
>g(x) : any | ||
>g : (x: Bar | Other) => any | ||
>x : { a: string; b: string; } | ||
|
||
g({ a: '', b: '' }) | ||
>g({ a: '', b: '' }) : any | ||
>g : (x: Bar | Other) => any | ||
>{ a: '', b: '' } : { a: string; b: string; } | ||
>a : string | ||
>'' : "" | ||
>b : string | ||
>'' : "" | ||
|
||
declare function h(x: Foo | Bar | Other): any; | ||
>h : (x: Foo | Bar | Other) => any | ||
>x : Foo | Bar | Other | ||
|
||
h(x); | ||
>h(x) : any | ||
>h : (x: Foo | Bar | Other) => any | ||
>x : { a: string; b: string; } | ||
|
||
h({ a: '', b: '' }) | ||
>h({ a: '', b: '' }) : any | ||
>h : (x: Foo | Bar | Other) => any | ||
>{ a: '', b: '' } : { a: string; b: string; } | ||
>a : string | ||
>'' : "" | ||
>b : string | ||
>'' : "" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
OverlappyType is a new favorite term.
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.
Another candidate was
getMostShallowSatisfiedType
but that sounded slightly judgemental.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.
Is there a general intuition for why the last most overlappy type is better than the first more overlappy type? For example, do people tend to write union types in a manner where the rightmost types tend to be better than the leftmost?
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.
So these types are always sorted by their ID, and those are always based on declaration order.
For named types (e.g. interface declarations) that's not usually that useful, but sometimes there's an intent when the user writes out anonymous types. For example, if the user wrote type literals such as
{ someProp: number } | { anotherProp: string }
, my own experience has shown me that a lot of the time the first one actually is thought of as the more common case. That's part of why I went with the first type in #26895 (though I think keeping it simple was the primary motivator).When I tried this and only took the first most-overlappy type, I think I found a decent number of hit-or-miss changes. I didn't see significant improvements in the baselines and given that the change is literally switching between a
>
and a>=
, I'd rather keep the baselines cleaner in that point. I think the high order bit is that we just look for overlappy types in the first place.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.
That all makes sense. this was more curiosity and if there was anything you guys had gleaned from patterns/practices that might weigh in here :)
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.
Oh right... otherwise it would be a pita to figure out equality. normalization totally makes sense here.
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.
Right, in our experience it ended up as more of a lavash or a naan, but I wouldn't be surprised if not sorting the IDs made type comparisons a pita.
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.
Barbari or Sangak 4ever.
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.
Please help @JsonFreeman
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.
I live in NYC now so I have to vote for bagels