Skip to content

Method overrides in derived classes can specify their parameters more specifically than their base class #27046

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

Closed
JakeTunaley opened this issue Sep 12, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@JakeTunaley
Copy link

JakeTunaley commented Sep 12, 2018

TypeScript Version: 3.1.0-dev.20180912

Search Terms: class extends assignability method parameters override

Code

class Base {
    public foo (x: number | string) {}
}

class Derived extends Base {
    public foo (x: number) {}
}

const value: Base = new Derived();
value.foo('');

Expected behavior:
An error on the method definition, or an error when value is assigned, when --strictFunctionTypes is enabled.

Actual behavior:
No error when --strictFunctionTypes is enabled.

As demonstrated, this means we can pass strings to value.foo, even though Derived.prototype.foo doesn't accept strings.

This issue precludes fixing #27047, as this bug breaks the contract it relies on.

Playground Link:
http://www.typescriptlang.org/play/#src=class%20Base%0D%0A%7B%0D%0A%09public%20foo%20(x%3A%20number%20%7C%20string)%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Derived%20extends%20Base%0D%0A%7B%0D%0A%09public%20foo%20(x%3A%20number)%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aconst%20v%3A%20Base%20%3D%20new%20Derived()%3B%0D%0Av.foo('')%3B

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 13, 2018

@DanielRosenwasser DanielRosenwasser added Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design Duplicate An existing issue was already created and removed Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design labels Sep 13, 2018
@JakeTunaley
Copy link
Author

@DanielRosenwasser Sorry, I forgot to specify in the original post that this occurs when --strictFunctionTypes is enabled. I've edited the post to reflect this.

@RyanCavanaugh
Copy link
Member

strictFunctionTypes doesn't apply to functions that were declared with method syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants