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

Differing results for inference produced when identical overload count changes #21733

Open
weswigham opened this issue Feb 7, 2018 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@weswigham
Copy link
Member

I happened to notice this while looking at something else inference related a bit ago. We already have a test that is looking for checking this, fixingTypeParametersRepeatedly3.ts, added in #2356, but it was accepted in #16368 (strict generic checks) with a (tiny, unnoticeable in all the other changes) baseline showing the fault.

Code

interface Base {
    baseProp;
}
interface Derived extends Base {
    toBase?(): Base;
}

var derived: Derived;

declare function foo<T>(x: T, func: (p: T) => T): T;
var result = foo(derived, d => d.toBase());

// bar should type check just like foo.
// result2 should have the same type as result
declare function bar<T>(x: T, func: (p: T) => T): T;
declare function bar<T>(x: T, func: (p: T) => T): T;
var result2 = bar(derived, d => d.toBase());

Expected behavior:
result and result2 are assigned the same type (there's a comment in the test asserting as much) - they are implemented identically except bar has an extra identical overload. (note: this can happen easily in the real world nowadays via intersection types! Intersecting multiple interfaces with similar base interfaces can cause exactly this situation.)

Actual behavior:
result typechecks as Derived
result2 typechecks as Base (seems wrong, since Base definitely doesn't have the toBase method used in the callback!)

Derived and Base are assignable to one another here, since toBase is optional, which is why the inference can succeed at all; however, Base, as reported in the second case can't actually have been the type used to typecheck the lambda body, as the toBase call doesn't cause an error in the test - I think the likely reason for the change in behavior is this removal, based on the content of the removed comment.

@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 7, 2018
@mhegazy mhegazy added this to the TypeScript 2.8 milestone Feb 7, 2018
@weswigham
Copy link
Member Author

weswigham commented Feb 7, 2018

Note: you can make the behavior between the two identical by removing the optimization within resolveCall that skips the subtype check on single-signature calls that's done prior to the assignability check. Unfortunately, they become identical in the bad behavior (both become a non-erroring Base) - that explains the difference; it's definitely because the first chooseOverload call with subtype relationship is failing (as expected) but still incorrectly permanently setting parameter types on the signature associated with the untyped lambda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants