TypeScript · Senior · Question 1 of 75
With strictFunctionTypes, can you assign (x: Dog) => void to a variable of type (x: Animal) => void?
- ✓Correct answer: No: function parameters are checked contravariantly
- 2Yes: function parameters are checked covariantly
- 3Yes, but only if
Dogis a class rather than an interface
Explanation
The variable promises to accept any Animal, but the function can only handle Dog - a call with Cat would break. So subtyping by parameter goes the opposite way: (x: Animal) => void can be assigned where (x: Dog) => void is expected, but not vice versa.