Skip to content

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

Closed
@asmundg

Description

@asmundg

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.

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