You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Working ExamplefunctionBaz(x: 'a'|'b'){vary:'a'=x}// This correctly errors:// Type '"a" | "b"' is not assignable to type '"a"'.// Type '"b"' is not assignable to type '"a"'.// Failing ExampleclassFoo{x(x:'a'|'b'):void{}}classBarextendsFoo{x(x:'a'):void{}}varfoo:Foo=newFoo()varbar:Foo=newBar()foo.x('b')bar.x('b')// Whoa there!
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.
The text was updated successfully, but these errors were encountered:
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
TypeScript Version: 2.2.2
Code
Playground link
Expected behavior:
The extension of class
Foo
byBar
should fail because the signatures of thex
method implementations are incompatible as illustrated in the last 4 lines.Foo.x
with'b'
is allowed.Foo
with instances ofBar
sinceBar
is a subclass ofFoo
Bar.x
does not accept'b'
as an argumentActual behavior:
Typescript doesn't complain and let's me extend
Foo
withBar
.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.The text was updated successfully, but these errors were encountered: