Skip to content

Instantly share code, notes, and snippets.

@FFKL
Last active March 18, 2020 20:04
Show Gist options
  • Select an option

  • Save FFKL/09f056b4e2c224cb8de20d745bc52921 to your computer and use it in GitHub Desktop.

Select an option

Save FFKL/09f056b4e2c224cb8de20d745bc52921 to your computer and use it in GitHub Desktop.
// @flow
class Animal {}
class Dog extends Animal {
bark() {}
}
class Cat extends Animal {
meow() {}
}
const dogs: Dog[] = [new Dog(), new Dog()];
const animals: Animal[] = dogs; // Cannot assign `dogs` to `animals` because `Dog` [1] is incompatible with `Animal` [2] in array element
animals.push(new Cat());
dogs.forEach(d => d.bark());
class Animal {}
class Dog extends Animal {
bark() {}
}
class Cat extends Animal {
meow() {}
}
const dogs: Dog[] = [new Dog(), new Dog()];
const animals: Animal[] = dogs;
animals.push(new Cat());
dogs.forEach(d => d.bark()); // TypeError: d.bark is not a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment