Javascript 返回参数否定的函数的假阳性谓词函数类型检入流

Javascript 返回参数否定的函数的假阳性谓词函数类型检入流,javascript,flowtype,Javascript,Flowtype,此代码: type Predicate = any => boolean; const xs = [1, 2, 3, 4, 5, 6]; const isEven = (x: number): boolean => x % 2 === 0; const filter = (pred: Predicate) => (xs: Array<number>) => xs.filter(pred); filter(isEven)(xs); 然后我得到Flow来显示

此代码:

type Predicate = any => boolean;

const xs = [1, 2, 3, 4, 5, 6];
const isEven = (x: number): boolean => x % 2 === 0;
const filter = (pred: Predicate) => (xs: Array<number>) => xs.filter(pred);

filter(isEven)(xs);
然后我得到Flow来显示一些相关的错误,就像它应该:

6: filter(x => -x)(xs);
                ^ Cannot call `filter` with function bound to `pred` because number [1] is incompatible with boolean [2] in the return value.
References:
1: type Predicate = number => boolean;
                    ^ [1]
1: type Predicate = number => boolean;
                              ^ [2]
同时,如果我试图使该函数返回更突出的类型,Flow就会发现问题。流正确地认为这些调用都包含类型错误:

filter(x => String(x))(xs); // <- string is incompatible with boolean, okay
filter(x => Number(x))(xs); // <- number is incompatible with boolean, also okay
filter(x => +x)(xs); // <- +x will always evaluate into a number or NaN, never a boolean
filter(x => x & x)(xs); // <- number is incompatible with boolean

filter(x=>String(x))(xs);//数字(x))(xs);//+x) (xs);//x&x)(xs);//-x
并获取流以显示错误?或者,在这种情况下,Flow是否正确地进行了类型检查,只是我遗漏了一些东西?

使用泛型类型,如下所示,在这里会有所帮助

type Result<T> = Array<T> => Array<T>;

const xs = [1, 2, 3, 4, 5, 6];
const filter = <T>(pred: (T) => boolean): Result<T> => xs => xs.filter(pred);

filter(x => -x)(xs);
type Result=Array=>Array;
常量xs=[1,2,3,4,5,6];
常量过滤器=(pred:(T)=>boolean):结果=>xs=>xs.filter(pred);
过滤器(x=>-x)(xs);
在flow中尝试:

filter(x => String(x))(xs); // <- string is incompatible with boolean, okay
filter(x => Number(x))(xs); // <- number is incompatible with boolean, also okay
filter(x => +x)(xs); // <- +x will always evaluate into a number or NaN, never a boolean
filter(x => x & x)(xs); // <- number is incompatible with boolean
type Result<T> = Array<T> => Array<T>;

const xs = [1, 2, 3, 4, 5, 6];
const filter = <T>(pred: (T) => boolean): Result<T> => xs => xs.filter(pred);

filter(x => -x)(xs);