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

Overridden method parameters are not checked for parameter contravariance #22156

Closed
ms-markda opened this issue Feb 23, 2018 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ms-markda
Copy link

TypeScript Version: 2.7.0-rc

Search Terms: class override method parameters covariance contravaraiance variance

Code

class Example {
    foo(maybe: number | undefined) { }
    str(str: string) { }
    compare(ex: Example) { }
}

class Override extends Example {
    foo(maybe: number) { } // Bad: should have error.
    str(str: 'override') { } // Bad: should have error.
    compare(ex: Override) { } // Bad: should have error.
}

const ex: Example = new Override();
ex.foo(undefined);
ex.str('anything');
ex.compare(new Example());

Expected behavior:

Definition of each overridden method should have an error. Parameter is contravariant instead of covariant.

Actual behavior:

No errors at compile time. Even with strictFunctionTypes enabled.

Playground Link:

https://www.typescriptlang.org/play/#src=class%20Example%20%7B%0D%0A%20%20%20%20foo(maybe%3A%20number%20%7C%20undefined)%20%7B%20%7D%0D%0A%20%20%20%20str(str%3A%20string)%20%7B%20%7D%0D%0A%20%20%20%20compare(ex%3A%20Example)%20%7B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Override%20extends%20Example%20%7B%0D%0A%20%20%20%20foo(maybe%3A%20number)%20%7B%20%7D%20%2F%2F%20Bad%3A%20should%20have%20error.%0D%0A%20%20%20%20str(str%3A%20'override')%20%7B%20%7D%20%2F%2F%20Bad%3A%20should%20have%20error.%0D%0A%20%20%20%20compare(ex%3A%20Override)%20%7B%20%7D%20%2F%2F%20Bad%3A%20should%20have%20error.%0D%0A%7D%0D%0A%0D%0Aconst%20ex%3A%20Example%20%3D%20new%20Override()%3B%0D%0Aex.foo(undefined)%3B%0D%0Aex.str('anything')%3B%0D%0Aex.compare(new%20Example())%3B

Related Issues:

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 24, 2018

It was a known design issue that method-style syntax doesn't enforce variance checks. If we did so, it would break all instances of things like Dog[] being assignable to Animal[], or the subclassing of HTMLDivElement from HTMLElement.

@DanielRosenwasser
Copy link
Member

See #18654 for more details.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 24, 2018
@DanielRosenwasser DanielRosenwasser changed the title Overridden method parameters are not checked for covariance Overridden method parameters are not checked for parameter contravariance Feb 24, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants