Content-Length: 275058 | pFad | http://github.com/microsoft/TypeScript/issues/22156

06 Overridden method parameters are not checked for parameter contravariance · Issue #22156 · microsoft/TypeScript · GitHub
Skip to content

Overridden method parameters are not checked for parameter contravariance #22156

Closed
@ms-markda

Description

@ms-markda

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: http://github.com/microsoft/TypeScript/issues/22156

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy