-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Merge overloads that only differ by return type when intersecting #30520
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf5b762
Merge overloads that only differ by return type when intersecting.
kring ca33f40
Fix lint, other style conformance.
kring 201c263
Update one test, add another.
kring 35f064d
Require identical type parameters to intersect call signatures.
kring 8896207
Merge branch 'master' into intersect-return-types
RyanCavanaugh 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 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
This file contains 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
61 changes: 61 additions & 0 deletions
61
tests/baselines/reference/intersectionOfCallsWithSameParameters.errors.txt
This file contains 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,61 @@ | ||
tests/cases/compiler/intersectionOfCallsWithSameParameters.ts(38,7): error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
tests/cases/compiler/intersectionOfCallsWithSameParameters.ts(39,7): error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
tests/cases/compiler/intersectionOfCallsWithSameParameters.ts(40,7): error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
tests/cases/compiler/intersectionOfCallsWithSameParameters.ts(41,7): error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
|
||
|
||
==== tests/cases/compiler/intersectionOfCallsWithSameParameters.ts (4 errors) ==== | ||
interface One { | ||
differentParameterType(id: string): { one: number }; | ||
differentNumberOfParameters(id: string): { one: number }; | ||
differentTypeParameterDefault<T = number>(id: string): { one: number }; | ||
differentTypeParameterConstraint<T extends { one: number }>(id: string): { one: number }; | ||
|
||
same1(id: string): { one: number }; | ||
same2<T>(id: string): { one: number }; | ||
same3<T extends { one: number }>(id: string): { one: number }; | ||
same4<T = number>(id: string): { one: number }; | ||
same5<T1 extends { one: number }, T2 = number>(id: string): { one: number }; | ||
} | ||
|
||
interface Two { | ||
differentParameterType(id: number): { two: number }; | ||
differentNumberOfParameters(id: string, second: string): { two: number }; | ||
differentTypeParameterDefault<T = string>(id: string): { two: number }; | ||
differentTypeParameterConstraint<T extends { two: number }>(id: string): { two: number }; | ||
|
||
same1(id: string): { two: number }; | ||
same2<T>(id: string): { two: number }; | ||
same3<T extends { one: number }>(id: string): { two: number }; | ||
same4<T = number>(id: string): { two: number }; | ||
same5<T1 extends { one: number }, T2 = number>(id: string): { two: number }; | ||
} | ||
|
||
const i: One & Two = <any>{}; | ||
|
||
// These lines should type check; the return type should be intersected. | ||
const same1: { one: number, two: number } = i.same1('test'); | ||
const same2: { one: number, two: number } = i.same2<number>('test'); | ||
const same3: { one: number, two: number } = i.same3<{ one:number }>('test'); | ||
const same4: { one: number, two: number } = i.same4('test'); | ||
const same5: { one: number, two: number } = i.same5<{ one:number }, string>('test'); | ||
|
||
// These lines should not, because the functions should become overloads rather | ||
// than the return types intersected. | ||
const differentParameterType: { one: number, two: number } = i.differentParameterType('test'); | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
!!! related TS2728 tests/cases/compiler/intersectionOfCallsWithSameParameters.ts:38:46: 'two' is declared here. | ||
const differentNumberOfParameters: { one: number, two: number } = i.differentNumberOfParameters('test'); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
!!! related TS2728 tests/cases/compiler/intersectionOfCallsWithSameParameters.ts:39:51: 'two' is declared here. | ||
const differentTypeParameterDefault: { one: number, two: number } = i.differentTypeParameterDefault('test'); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
!!! related TS2728 tests/cases/compiler/intersectionOfCallsWithSameParameters.ts:40:53: 'two' is declared here. | ||
const differentTypeParameterConstraint: { one: number, two: number } = i.differentTypeParameterConstraint<{ one: number }>('test'); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2741: Property 'two' is missing in type '{ one: number; }' but required in type '{ one: number; two: number; }'. | ||
!!! related TS2728 tests/cases/compiler/intersectionOfCallsWithSameParameters.ts:41:56: 'two' is declared here. | ||
|
59 changes: 59 additions & 0 deletions
59
tests/baselines/reference/intersectionOfCallsWithSameParameters.js
This file contains 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,59 @@ | ||
//// [intersectionOfCallsWithSameParameters.ts] | ||
interface One { | ||
differentParameterType(id: string): { one: number }; | ||
differentNumberOfParameters(id: string): { one: number }; | ||
differentTypeParameterDefault<T = number>(id: string): { one: number }; | ||
differentTypeParameterConstraint<T extends { one: number }>(id: string): { one: number }; | ||
|
||
same1(id: string): { one: number }; | ||
same2<T>(id: string): { one: number }; | ||
same3<T extends { one: number }>(id: string): { one: number }; | ||
same4<T = number>(id: string): { one: number }; | ||
same5<T1 extends { one: number }, T2 = number>(id: string): { one: number }; | ||
} | ||
|
||
interface Two { | ||
differentParameterType(id: number): { two: number }; | ||
differentNumberOfParameters(id: string, second: string): { two: number }; | ||
differentTypeParameterDefault<T = string>(id: string): { two: number }; | ||
differentTypeParameterConstraint<T extends { two: number }>(id: string): { two: number }; | ||
|
||
same1(id: string): { two: number }; | ||
same2<T>(id: string): { two: number }; | ||
same3<T extends { one: number }>(id: string): { two: number }; | ||
same4<T = number>(id: string): { two: number }; | ||
same5<T1 extends { one: number }, T2 = number>(id: string): { two: number }; | ||
} | ||
|
||
const i: One & Two = <any>{}; | ||
|
||
// These lines should type check; the return type should be intersected. | ||
const same1: { one: number, two: number } = i.same1('test'); | ||
const same2: { one: number, two: number } = i.same2<number>('test'); | ||
const same3: { one: number, two: number } = i.same3<{ one:number }>('test'); | ||
const same4: { one: number, two: number } = i.same4('test'); | ||
const same5: { one: number, two: number } = i.same5<{ one:number }, string>('test'); | ||
|
||
// These lines should not, because the functions should become overloads rather | ||
// than the return types intersected. | ||
const differentParameterType: { one: number, two: number } = i.differentParameterType('test'); | ||
const differentNumberOfParameters: { one: number, two: number } = i.differentNumberOfParameters('test'); | ||
const differentTypeParameterDefault: { one: number, two: number } = i.differentTypeParameterDefault('test'); | ||
const differentTypeParameterConstraint: { one: number, two: number } = i.differentTypeParameterConstraint<{ one: number }>('test'); | ||
|
||
|
||
//// [intersectionOfCallsWithSameParameters.js] | ||
"use strict"; | ||
var i = {}; | ||
// These lines should type check; the return type should be intersected. | ||
var same1 = i.same1('test'); | ||
var same2 = i.same2('test'); | ||
var same3 = i.same3('test'); | ||
var same4 = i.same4('test'); | ||
var same5 = i.same5('test'); | ||
// These lines should not, because the functions should become overloads rather | ||
// than the return types intersected. | ||
var differentParameterType = i.differentParameterType('test'); | ||
var differentNumberOfParameters = i.differentNumberOfParameters('test'); | ||
var differentTypeParameterDefault = i.differentTypeParameterDefault('test'); | ||
var differentTypeParameterConstraint = i.differentTypeParameterConstraint('test'); |
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.
I wouldn't call this quite right. This has lost the nuance of which type the signatures originally came from. Say I wrote:
with this logic, when I called something of type
WhyTho
, the result would be of type{x;} & {y;}
, even though at no point did I actually intersect multiple callable things. Like our recent union calling infrastructure, this needs to operate pairwise over the input overload lists, and only merge between types. Eg, if I writeI expect to see a resulting overload list along the lines of
it'd be a different story if for overload resolution we always resolved all applicable overloads "simultaneously" and merged the result, but unfortunately we always pick the first applicable overload, so retaining overload list order is important to retaining consistent behavior.
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.
Thanks for taking a look @weswigham! I thought of that while implementing this, but I couldn't think of a way that a function could satisfy the the type
WhyTho
without also satisfying the intersected form produced by my change. For example:The error on the last line is:
How can that error by resolved other than by changing
asdf
to return the intersected type? I think that's why most languages I'm familiar with consider it an error in the first place to have overloads that differ only be return type.Still, I agree it's undesirable that the signature changes just because of an unrelated intersection. Assuming it wouldn't be acceptable to always intersect return types of signatures with identical parameters (even in the absence of an explicit intersection), I'll see if I can change the code to only intersect return types of signatures from different intersected types. Any advice in that regard is greatly appreciated of course!
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.
To answer my own question:
It can be resolved by declaring an overload, of course!
It kind of makes my brain hurt to figure out what those two overloads with identical parameters are supposed to tell us about that function. It sort of just seems like it should be an error, but I'm sure there are good reasons that it's not.
So moving on, I'm implementing the change to only intersect return types across types, never within a type, and it seems mostly straightforward so far. I just want to clarify what should happen in a slightly more complicated situation than the one you mentioned:
I think in this case, it should create no-parameters signatures which are the Cartesian product of the no-parameters signatures in the intersected types. So:
Does that seem right to you?
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 agree with the premise, but also disagree with doing it for the same reason we don't combine multiple multi-overload sets when manufacturing union signatures: overload order matters, and the "correct" ordering of the interleaved product of signatures is ambiguous. So long as overload order matters, we can't actually combine multiple ordered lists (since the only "correct" behavior would be to resolve on each type of the union/intersection separately and combine the single selected signature).
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've been giving this a fair bit of thought over the past few days, and, if I'm understanding you correctly, we need to make sure that any change to signature intersection follows these rules.
Upon intersection:
In addition, there may be one more rule:
3. The overloads of an intersection type must occur in the order of intersection of their source types.
If all three of these rules must hold, the result of an intersection will have to be pretty much as it is today, except that if two overloads that differ only by return type happen to be right next to each other in the list (as currently computed in master), and they come from different types, then they can be turned into a single overload by intersecting their return types.
So in this example:
The result would be:
However, if we can relax rule 3, we could achieve something like I think you were getting at with this statement:
If I'm understanding you correctly, you're saying the only correct algorithm would be this: given a function call on an intersected type, we perform overload resolution on all of the constituent intersected types independently. Some may resolve successfully, some may not. Put all the successful ones in a list in the same order as the types they came from. Then, select the first signature from the list and intersect its return type with all other signatures in the list that differ from the first one only be return type.
That won't really uphold rule 3. Given the type above and this call site:
We select
(): {y;}
from the first type in the intersection and(): {x;}
from the second type, so the intersected signature would be(): {y;} & {x;}
. That puts precedence on(): {x;}
from the second type over(): {q;}
from the first type, violating rule 3.But if that's ok (I think it is! It's less surprising than the
(): {y;}
we get when we strictly enforce rule 3), then we can achieve the same effect as this call-site algorithm at intersection-time with an algorithm like this:Which for the type above would yield:
Which seems quite sensible to me.
Does that make sense to you?
I've been studying your recent union changes (#29011), trying to understand how that approach would apply to the intersection case. I've learned a lot about how to actually compare the signatures, but less obvious is how the intersection should behave.
By the way, I find this interesting and am happy to keep working on it even if that involves a bunch of false starts and iteration. Do let me know if I'm just wasting your time, though. There's no point in doing that. :)