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

Method overriding rules might be too permissive #5786

Closed
tobich opened this issue Nov 25, 2015 · 1 comment
Closed

Method overriding rules might be too permissive #5786

tobich opened this issue Nov 25, 2015 · 1 comment
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@tobich
Copy link

tobich commented Nov 25, 2015

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.
@mhegazy
Copy link
Contributor

mhegazy commented Mar 28, 2016

Please see https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-function-parameters-bivariant for relevant details.

@mhegazy mhegazy closed this as completed Mar 28, 2016
@mhegazy mhegazy added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Mar 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants