流数组类型在该类型的子集上失败 [英] Flow Array type fails on subset of that type

查看:36
本文介绍了流数组类型在该类型的子集上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可在许多不同地方使用的函数,因此它具有非常广泛的类型:

I have a function that is used in a lot of different places, so it has a very broad typing:

export const stringToArrayFn = ( accessor?: | Function | string | boolean | Object | Array<Function | string | boolean | Object>, defaultAccessor?: Function, raw?: boolean )

export const stringToArrayFn = ( accessor?: | Function | string | boolean | Object | Array<Function | string | boolean | Object>, defaultAccessor?: Function, raw?: boolean )

当我打电话时:

stringToArrayFn(yAccessor)

将yAccessor键入为:

With yAccessor being typed as:

yAccessor?: Array<Function | string> | Function | string

流程抱怨Cannot call stringToArrayFn with yAccessor bound to accessor because boolean [1] is incompatible with string [2] in array element.

Flow complains Cannot call stringToArrayFn with yAccessor bound to accessor because boolean [1] is incompatible with string [2] in array element.

我不明白,因为yAccessor中的Array是stringToArrayFn中定义的数组可能性的有效子集(前者仅允许函数或字符串的数组,后者允许函数,字符串,布尔和对象的数组).

I don't get it because the Array in yAccessor is a valid subset of the array possibilities defined in stringToArrayFn (the former only allows arrays of function or string, the latter allows arrays of functions, strings, bools and objects).

一个人如何处理这种打字?

How does one handle this kind of typing?

推荐答案

stringToArrayFn可能会调用accessor.push(true)并导致您的Array<Function | string>包含boolean,这很糟糕.

stringToArrayFn could call accessor.push(true) and cause your Array<Function | string> to contain a boolean, which would be bad.

如果stringToArrayFn不想改变提供的数组,请在stringToArrayFn的定义中使用$ReadOnlyArray代替Array.

Use $ReadOnlyArray instead of Array in the definition of stringToArrayFn if it doesn't intend to mutate the provided array.

这篇关于流数组类型在该类型的子集上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆