協變(Covariant)、逆變(Contravariant)、雙向協變(Bivariant)並非Typescript所特有,其他結構化語言諸如c#、java等也都擁有該特性。
怎么理解這個概念呢? 先說說集合、超集、子集(set, superset, subset)
下圖中有兩個集合:脊索動物、哺乳動物。 哺乳動物一定是脊索動物,反之則不一定。 因此我們說脊索動物是哺乳動物的超集,哺乳動物是脊索動物的子集。
哺乳動物一定具有脊索動物的特性,反之則不一定。
協變
協變是指:子集能賦值給其超集。
class Chordate {
hasSpine(): boolean {
return true;
}
}
class Mammal extends Chordate {
canBreastFeed(): boolean {
return true;
}
}
function foo(animal: Chordate){
animal.hasSpine();
}
foo(new Chordate());
foo(new Mammal());
以上代碼證明了Typescript支持協變,Mammal是Chordate的子集,方法foo接受參數類型為Chordate,而Mammal實例也能賦值給Chordate參數。
逆變
逆變(Contravariance)與雙變(Bivariance)只針對函數有效。 --strictFunctionTypes 開啟時只支持逆變,關閉時支持雙變。
class Chordate {
hasSpine(): boolean {
return true;
}
}
class Mammal extends Chordate {
canBreastFeed(): boolean {
return true;
}
}
declare let f1: (x: Chordate) => void;
declare let f2: (x: Mammal) => void;
f2=f1;
f1=f2; //Error: Mammal is incompatible with Chordate
協變比較好理解,為什么函數賦值,只能支持逆變(默認),而不支持協變呢? 請看以下代碼示例
class Animal {
doAnimalThing(): void {
console.log("I am a Animal!")
}
}
class Dog extends Animal {
doDogThing(): void {
console.log("I am a Dog!")
}
}
class Cat extends Animal {
doCatThing(): void {
console.log("I am a Cat!")
}
}
function makeAnimalAction(animalAction: (animal: Animal) => void) : void {
let cat: Cat = new Cat()
animalAction(cat)
}
function dogAction(dog: Dog) {
dog.doDogThing()
}
makeAnimalAction(dogAction) // TS Error at compilation, since we are trying to use `doDogThing()` to a `Cat`
上述代碼說明了如果函數賦值支持協變的話,有可能會導致bug
參考:
https://codethrasher.com/post/2019-08-28-type-variance-and-typescript/
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html
https://dev.to/codeozz/how-i-understand-covariance-contravariance-in-typescript-2766