Skip to content

Order of method overloading declaration matters when one parameter type extends the other #1860

Description

@hraban

Playing around with the type definitions for React 0.13 I ran into this problem:

declare module Test {
    interface Parent {
        y: number;
    }
    interface Child extends Parent {
        z: string;
    }

    interface ParentDeriv {
        yderiv: number;
    }
    interface ChildDeriv extends ParentDeriv {
        zderiv: string;
    }

    interface Api {
        makeParent(): Parent;
        makeChild(): Child;
        meth(element: Parent): ParentDeriv;
        meth(element: Child): ChildDeriv;
    }

    var api: Api;
}

var x = Test.api.meth(Test.api.makeParent());
x.zderiv; // Should be: error. Is: error.
var y = Test.api.meth(Test.api.makeChild()); // takes wrong method.
y.zderiv; // Should be: ok. Is: error. y is inferred ParentDeriv.

But if you flip the method declarations around, it works fine:

declare module Test {
    interface Parent {
        y: number;
    }
    interface Child extends Parent {
        z: string;
    }

    interface ParentDeriv {
        yderiv: number;
    }
    interface ChildDeriv extends ParentDeriv {
        zderiv: string;
    }

    interface Api {
        makeParent(): Parent;
        makeChild(): Child;
        // Note order: child class first
        meth(element: Child): ChildDeriv;
        meth(element: Parent): ParentDeriv;
    }

    var api: Api;
}

var x = Test.api.meth(Test.api.makeParent());
x.zderiv; // Should be: error. Is: error.
var y = Test.api.meth(Test.api.makeChild());
y.zderiv; // Should be: ok. Is: ok.

This is a bug, right? With TS I'm never sure..

Tested with 1.4 (Playground and local)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions