question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FAQs: bivariance example is wrong

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DanielRosenwassercommented, Jul 28, 2021

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.

cloneAnimal<Animal>(c, trainDog); // no error :(

cloneAnimal<Dog>(c, trainDog); // error :)
0reactions
DanielRosenwassercommented, Jul 28, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found