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

Type 'a'|'b' is assignable to 'a' in overwriting method signature of subclass #15391

Closed
janv opened this issue Apr 26, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@janv
Copy link

janv commented Apr 26, 2017

TypeScript Version: 2.2.2

Code

// Working Example
function Baz(x: 'a' | 'b') {
    var y:'a' = x
}
// This correctly errors:
// Type '"a" | "b"' is not assignable to type '"a"'.
//  Type '"b"' is not assignable to type '"a"'.

// Failing Example
class Foo {
  x(x:'a'|'b'):void {}
}

class Bar extends Foo {
  x(x:'a'):void {}
}

var foo:Foo = new Foo()
var bar:Foo = new Bar()

foo.x('b')
bar.x('b') // Whoa there!

Playground link

Expected behavior:
The extension of class Foo by Bar should fail because the signatures of the x method implementations are incompatible as illustrated in the last 4 lines.

  • Calling Foo.x with 'b' is allowed.
  • We should be able to transparently substitute instances of Foo with instances of Bar since Bar is a subclass of Foo
  • But Bar.x does not accept 'b' as an argument

Actual behavior:

Typescript doesn't complain and let's me extend Foo with Bar.

As seen in the Baz function, checking assignability of Unions seems to work in other cases but apparently not in the case of method overwriting by a subclass.

@janv janv changed the title Type 'a'|'b' is assignable to 'a' but shouldn't Type 'a'|'b' is assignable to 'a' in overwriting method signature of subclass Apr 26, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2017

@janv janv closed this as completed Apr 27, 2017
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 27, 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants