flow icon indicating copy to clipboard operation
flow copied to clipboard

Possibility to call non-existing method on array element without explicit 'any'

Open Renegatto opened this issue 4 years ago • 0 comments

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).

Renegatto avatar Feb 20 '21 13:02 Renegatto