What does the following generic constraint pattern achieve? type Covariant = () => T; type Contravariant = (arg: T) => void; // Which is wider: Covariant or Covariant if Dog extends Animal?

TypeScript Professional Hard

TypeScript Professional — Hard

What does the following generic constraint pattern achieve? type Covariant = () => T; type Contravariant = (arg: T) => void; // Which is wider: Covariant or Covariant if Dog extends Animal?

Key points

  • Covariant is wider due to the relationship between subtypes and supertypes
  • Contravariant is narrower as it expects a specific type
  • The generic types in TypeScript can have different constraints based on their usage

Ready to go further?

Related questions