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
Expected behavior:
Should compile without error, allowing me to specify that doSomething can accept a type of only Woof or Meow as a parameter and should output that type as the return value.
Actual behavior:
Yields the following error from compiler (for Dog and a similar error for Cat):
Class 'Dog' incorrectly implements interface 'Animal'.
Types of property 'doSomething' are incompatible.
Type '(sound: Woof) => Woof' is not assignable to a type '(input: T) => T'.
Types of parameters 'sound' and 'sound' are incompatible.
Type 'T' is not assignable to type 'Woof'.
Type 'Woof | Meow' is not assignable to type 'Woof'.
Type 'Meow' is not assignable to type 'Woof'.
Property 'a' is missing in type 'Meow'
Workaround:
Moving the generic constraint to the Animal interface and off the function itself resolves the issue as in the following:
Additional Notes
Variations of problem code not working at this SO question
Also, problem code works fine in TypeScript Playground, just produces errors when compiled locally (and in VSCode).
The text was updated successfully, but these errors were encountered:
WhitWaldo
changed the title
Inferring generic type constraint works on interface, but not function within interface
Generic type constraint works on interface, but not function within interface
Jul 9, 2017
When you write doSomething: <T extends Woof | Meow>(input: T) => T; , it specifies that doSomething must be a function generic in T. So if you have a: Animal you can call a.doSomething<Woof>(woof) safely. But if a was a cat that would not be safe. #16368 started checking generic signatures, so we will now give you errors in this case.
In contrast, when you declare Cat implements Animal<Meow>, it's better, because you can' just have an a: Animal, you would have to specify the T.
Unfortunately, due to function bivariance, we would allow you to pass let a: Animal<Meow | Wolf> = new Cat(). There are many issues about that, such as #10717, #12498, #9825, #16735, #14973.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.4.1
Code:
Expected behavior:
Should compile without error, allowing me to specify that doSomething can accept a type of only Woof or Meow as a parameter and should output that type as the return value.
Actual behavior:
Yields the following error from compiler (for Dog and a similar error for Cat):
Class 'Dog' incorrectly implements interface 'Animal'.
Types of property 'doSomething' are incompatible.
Type '(sound: Woof) => Woof' is not assignable to a type '(input: T) => T'.
Types of parameters 'sound' and 'sound' are incompatible.
Type 'T' is not assignable to type 'Woof'.
Type 'Woof | Meow' is not assignable to type 'Woof'.
Type 'Meow' is not assignable to type 'Woof'.
Property 'a' is missing in type 'Meow'
Workaround:
Moving the generic constraint to the Animal interface and off the function itself resolves the issue as in the following:
Additional Notes
Variations of problem code not working at this SO question
Also, problem code works fine in TypeScript Playground, just produces errors when compiled locally (and in VSCode).
The text was updated successfully, but these errors were encountered: