Skip to content

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

@Renegatto

Description

@Renegatto

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typing: soundnessNo false negatives (type checker claims that there is no error in the incorrect program)bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions