flow icon indicating copy to clipboard operation
flow copied to clipboard

Array.prototype.filter on non-refined nullable elements leads to unhelpful error message

Open flex-tjon opened this issue 1 year ago • 2 comments

Flow version: 0.201.0

Expected behavior

Flow points out that documents is an array of nullable elements and filteredDocuments is an array of non-null elements, which requires a filter/refinement in order to satisfy that type constraint

Actual behavior

Flow emits an error that looks like:

56:         .filter((document) => {
             ^ Cannot call `documents.filter(...).filter` because property `name` is missing in function [1] but exists in statics of `Boolean` [2]. [incompatible-call]
References:
56:         .filter((document) => {
                    ^ [1]
[LIB] ..//flow/v0.201.0/flowlib/core.js:367: declare class Boolean {

This seems related to filter being an overloaded method type to support both .filter(Boolean) and the usual callback definitions, but this error makes it difficult to find the root cause of the issue. Annotating the filter callback params doesn't seem to fix that either.

  • Link to Try-Flow or Github repo: Try Flow

flex-tjon avatar Mar 06 '23 23:03 flex-tjon

Maybe the root cause for this is similar to that of #9015?

flex-tjon avatar Mar 06 '23 23:03 flex-tjon

Yeah the root cause is the same. When we are unable to pick an overload, we might get these bad error messages.

SamChou19815 avatar Mar 07 '23 00:03 SamChou19815