What is bivariant parameter? ~ TypeScript
Asked Answered
T

1

9

The TypeScript's docs comes with Compiler Options, where the --strictFunctionTypes is defined with its description. Unfortunately, can't get what is bivariant parameter. I tried to understand https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-function-parameters-bivariant as well.

Trek answered 14/8, 2019 at 17:31 Comment(0)
F
7

A "bivariant" parameter is one that is both covariant and contravariant.

The TypeScript 2.6 release notes describe the --strictFunctionTypes compiler option well, since that's the first release that supported it.

Under --strictFunctionTypes function type parameter positions are checked contravariantly instead of bivariantly. For some background on what variance means for function types check out What are covariance and contravariance?.

This article linked in that quote describes the difference between covariance and contravariance: https://www.stephanboyer.com/post/132/what-are-covariance-and-contravariance

Foucault answered 14/8, 2019 at 17:45 Comment(1)
The last links with this 2 sentences "Suppose I need a List<Animal> and you pass me a List<Dog>. Since I think I have a List<Animal>, I might try to insert a Cat into it. Now your List<Dog> has a Cat in it! The type system should not allow this." describes it quite alright.Trek

© 2022 - 2024 — McMap. All rights reserved.