0
0
Typescriptprogramming~5 mins

Covariance and contravariance in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is covariance in TypeScript?
Covariance means you can use a more specific type than expected. For example, if a function expects a type, you can give it a subtype. It works like a "child" fitting where a "parent" is expected.
Click to reveal answer
beginner
What is contravariance in TypeScript?
Contravariance means you can use a more general type than expected. For example, if a function expects a subtype, you can give it a supertype. It is like accepting a broader category where a specific one is expected.
Click to reveal answer
intermediate
Explain covariance with function return types.
Function return types are covariant. This means a function that returns a subtype can be used where a function returning a supertype is expected. It is safe because the caller expects a general type but gets a more specific one.
Click to reveal answer
intermediate
Explain contravariance with function parameter types.
Function parameter types are contravariant. This means a function that accepts a supertype can be used where a function expecting a subtype is required. It is safe because the function can handle more general inputs.
Click to reveal answer
beginner
Why is understanding covariance and contravariance important in TypeScript?
It helps you write flexible and safe code. You can substitute types correctly in functions and variables without errors. It also helps understand how TypeScript checks type compatibility.
Click to reveal answer
Which of the following is an example of covariance in TypeScript?
AA function returning a supertype where a subtype is expected
BA function accepting a supertype where a subtype is expected
CA function accepting a subtype where a supertype is expected
DA function returning a subtype where a supertype is expected
Contravariance in TypeScript applies mainly to:
AFunction parameter types
BClass inheritance
CVariable declarations
DFunction return types
If a function expects a parameter of type 'Dog', which parameter type can be used contravariantly?
AAnimal (supertype of Dog)
BDog
CCat (unrelated type)
Dstring
Why is covariance safe for function return types?
ABecause the caller expects a more specific type
BBecause the caller expects a general type and gets a more specific one
CBecause the function accepts more general inputs
DBecause the function accepts more specific inputs
Which statement about TypeScript variance is true?
AFunction parameters are covariant
BFunction return types are contravariant
CFunction parameters are contravariant
DVariables are always invariant
Describe covariance and contravariance in your own words with simple examples.
Think about how types can be substituted in function parameters and return values.
You got /3 concepts.
    Explain why function return types are covariant and function parameter types are contravariant in TypeScript.
    Consider what happens if you give a function a more general or more specific type.
    You got /3 concepts.