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
Type '(b: B) => void' is not assignable to type '<C extends A>(c: C) => void'.
Types of parameters 'b' and 'c' are incompatible.
Type 'C' is not assignable to type 'B'.
Type 'A' is not assignable to type 'B'.
Property 'b' is missing in type 'A'.
Comment
Weirdly, it doesn't show any errors on Playground, but it shows errors when compiled on local project.
The text was updated successfully, but these errors were encountered:
remagpie
changed the title
Type error with 'extends' constraint and
Type error with 'extends' constraint and inheritance
Jul 20, 2017
The error message is a good one. If I change your implementation of x() to actually use the b parameter:
functionx(b: B): void{console.log(b.b.toString());// expects b.b to be defined}consty: <CextendsA>(c: C)=>void=x;
Then you can see why assigning x to y is flagged. You can apparently call y() on any A, but this will blow up at runtime unless the parameter is a B:
y({'a': 2});// no error in TS, but TypeError at runtime
EDIT: of course, your next question might be: given that argument, why doesn't the following throw a type error?
consty: (a: A)=>void=x;// just fine apparently
The answer is: 🤷. Parameter bivariance is unsound and makes me sad, but I guess it allows for some useful programming patterns. See #14973 for some discussion and pointers to docs.
TypeScript Version: 2.4.1
Code
Expected behavior:
No Type Error
Actual behavior:
Type error with message :
Comment
Weirdly, it doesn't show any errors on Playground, but it shows errors when compiled on local project.
The text was updated successfully, but these errors were encountered: