Repository: TypeScript
Issue: FAQs: bivariance example is wrong
Labels: Docs

# Bug Report

the example provided [here](https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-function-parameters-bivariant) is not a clear example of bivariance:

```ts
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`:
```ts
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:
```ts
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

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?strict=false&noImplicitAny=false&strictNullChecks=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false#code/MYGwhgzhAECCB2BLAtmE0DeAoavoHc0BrACgEpNoBfLGrUSGAEQHsBzaAUwA8AXT+ABMYCFGkw48AIzAAnUhQzVaWeuCjQAwmF5c+A4XCSp02PNGScW+cpRp1BnBrM7QAZgFd4wXohbxoXlkwRHhWNhJBAC5ocLIYgDcWREEsR2dXT29ff2hQf05RExIIFg9ZYE4YorQAGmhBApiSFwgPEF5q4zQKAF4APmgklPih5NT08Bd3Lx8-APz4Qu6QAHEBTllEYAAeABU9fiERFf6Ssoqq6D36xqXm1vbO677B4cFR96wQTl1gaF60CW+C0OnIAG5VAB6KHQABKXl8li4slkLFk0AAFptXCxgMByjApE4wB4IK58K4DNAPAAHaChJJEUIcADkQRCYXYrIIiF4mOgYGgrO0vFZqkWyzEIBIwHqHNCcUhWBheRYyFpiB+KLRsmqsjYHks8F0LDcgQAnrTXKzIjE4gC3uMeYgYPAWLpGIg2PAwFJtbwWNBaXIwJZ+BizZbrcKWpw2h0YqLXmMUuLJTU1hstsBZfLgor2GRwUA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
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);
```