Skip to content

Method overriding rules might be too permissive #5786

Description

@tobich

Consider this example:

class T1 {
    prop1: string = 'T1/prop1';
}

class T2 extends T1 {
    prop2: string = 'T2/prop2';
}

class A {
    propA: string;
    checkValue(value: T1) {
        console.assert(!!value.prop1);
    }
}

class B extends A {
    propB: string;
    checkValue(value: T2) {
        console.assert(!!value.prop2);
    }
}

B::checkValue(T2) can override A::checkValue(T1) because T2 is assignable to T1 (as it is its subclass).

However, following code, while perfectly valid, will break the second assertion:

const a: A = new B();
const t1: T1 = new T1();
a.checkValue(t1); // B.prototype.checkValue(), expecting T2 but getting T1.

Activity

  1. mhegazy commented on Mar 28, 2016

    @mhegazy
    Contributor
  2. locked and limited conversation to collaborators on Jun 19, 2018
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