TypeScript · Senior · Question 1 of 75

With strictFunctionTypes, can you assign (x: Dog) => void to a variable of type (x: Animal) => void?

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.

Further reading

Problem with a question

TypeScript · Senior · question 1

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz