flow
flow copied to clipboard
Possibility to call non-existing method on array element without explicit 'any'
Flow version: 0.145.0
/* @flow */
class Animal { }
class Dog extends Animal { bark() { } }
class Cat extends Animal { meow() { } }
const arr1 = [new Dog(), new Dog()];
const arr2: Animal[] = arr1;
arr2.push(new Cat()); // arr1 == [new Dog(), new Dog(), new Cat()]
arr1.forEach(c => c.bark()); //will fail at runtime with 'c.bark is not a function'
Expected behavior
Any sort of static checking error.
Actual behavior
JS runtime error: c.bark is not a function
Adding explicit type annotation to arr1
leads to type error (expected behavior).
- Link to Try-Flow: Try Flow reproduction