FAQs: bivariance example is wrong
See original GitHub issueBug Report
the example provided here is not a clear example of bivariance:
function trainDog(d: Dog) { ... }
function cloneAnimal(source: Animal, done: (result: Animal) => void): void { ... }
let c = new Cat();
// Runtime error here occurs because we end up invoking 'trainDog' with a 'Cat'
cloneAnimal(c, trainDog);
this isn’t really a problem with bivariance, and can be solved by simply using a generic on cloneAnimal
:
function cloneAnimal<T extends Animal>(source: T, done: (result: T) => void) { ... }
we now correctly receive a compile error, even with all the strictness flags disabled
i would suggest using the following example instead:
class Animal {
walk() { }
}
class Dog extends Animal {
bark() { }
}
class Cat extends Animal {
meow() { }
}
class List<T> {
constructor(public values: T[]) {}
add(value: T) {
this.values.push(value)
}
}
const cats: List<Cat> = new List([new Cat()])
const animals: List<Animal> = cats
animals.add(new Dog())
// runtime error, because the list of cats now has a dog in it
cats.values[1].meow()
🔎 Search Terms
faq bivariance
⏯ Playground Link
Playground link with relevant code
💻 Code
class Animal {
walk() { }
}
class Dog extends Animal {
bark() { }
}
class Cat extends Animal {
meow() { }
}
declare function trainDog(d: Dog): void
declare function cloneAnimal(source: Animal, done: (result: Animal) => void): void
declare function cloneAnimalGeneric<T extends Animal>(source: T, done: (result: T) => void): void
let c = new Cat();
// Runtime error here occurs because we end up invoking 'trainDog' with a 'Cat'
cloneAnimal(c, trainDog);
// compile error: Argument of type '(d: Dog) => void' is not assignable to parameter of type '(result: Cat) => void'
cloneAnimalGeneric(c, trainDog);
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
FAQ · microsoft/TypeScript Wiki - GitHub
This is isomorphic to the example that "wanted" an error. At runtime, forEach invokes the given callback with three arguments (value, index, ...
Read more >Difference between Variance, Covariance, Contravariance ...
Could you please explain using small and simple TypeScript examples what is Variance, Covariance, Contravariance and Bivariance?
Read more >Type Systems: Covariance, Contravariance, Bivariance, and ...
Variance is a topic that comes up fairly often in type systems and can be a bit confusing the first time you hear...
Read more >Bivariate Data: Examples, Definition and Analysis - Intellspot
A list of bivariate data examples: including linear bivariate regression analysis, correlation (relationship), distribution, and scatter plot.
Read more >Bivariate Data Analysis & Examples - Video & Lesson Transcript
It is used to help determine whether a particular research finding represents a real-world quality or some type of statistical fluke or error....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The fact that we consider “contravariant” inference positions to be higher-priority in type argument inference is definitely meant to prevent bivariance issues, but what @fatcerberus said is right. You can easily defeat it with an explicit type argument.
I’m very confused right now -
strictFunctionTypes
should be a strict-mode flag!The only reason that I can think of why that might not appear to be the case (i.e. the only “wat” there) is that only non-method-like functions have their parameters checked strictly.