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

Union type method parameters in base class accepts incompatible types in subclass overrides #19832

Closed
asmundg opened this issue Nov 8, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@asmundg
Copy link
Member

asmundg commented Nov 8, 2017

Using union types in base class method declarations allows override functions that only declare one of the union member types. This produces code that crashes at runtime, even with --strict.

TypeScript Version: 2.7.0-dev.20171108

Code

abstract class Base {
    abstract foo(arg: string | number): boolean
}

class C extends Base {
    foo(arg: string): boolean {
        return arg.startsWith("a")
    };
}

const a: Base = new C()
a.foo(1)

Expected behavior:

Should not compile.

Actual behavior:

Compiles and crashes at runtime.

@ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 8, 2017
@asmundg
Copy link
Member Author

asmundg commented Nov 8, 2017

Aha, I had missed the part where --strictFunctionTypes does not apply to methods. I take it I should stay away from inheritance if I want contravariant parameter checking then.

@asmundg asmundg closed this as completed Nov 8, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

2 participants