Skip to content

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

Open
@weswigham

Description

@weswigham

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions