typescript - Typescript函数数组类型检查
问题描述
代码很简单
// it is ok because type 'string' is assignable to type 'string | number'
const arr:Array<string|number> = []
arr.push('foo')
但我收到以下代码错误。
interface Fn<T> {
(val: T): T
}
const fnArr:Fn<string|number>[] = []
const fn = (str:string)=>str
//error
fnArr.push(fn)
错误显示:
Argument of type '(str: string) => string' is not assignable to parameter of type 'Fn<string | number>'.
Types of parameters 'str' and 'val' are incompatible.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.(2345)
逻辑似乎相反。fnArr 被声明为一个数组,其元素是带有字符串或数字参数的函数,因此推送带有字符串参数的函数是有效的。我想 'string' is assignable to type 'string | number'
,但为什么错误显示Type 'string | number' is not assignable to type 'string'.
TS 处理数组元素类型检查时的规则是什么?将元素类型分配给声明的数组类型或将声明的数组类型分配给元素类型?
编辑:
阅读@Aleksey 提到的文档后,我仍然感到有些困惑。我是说功能
(val: string | number) => string | number
val
有typestring
或 type的参数number
,我认为它比函数只有 type 的参数更大string
。就像一个比喻。我认为 typenumber
扩展了 type number | string
。所以我认为上面的代码不应该有任何错误
相反,正如@Aleksey 所说,函数
(val: string | number) => string | number
可以处理 number 和 string 类型的参数,但是函数(val: string) => string
不能处理 type 的参数number
,我认为他是正确的。
所以我仍然感到困惑。
解决方案
所以这里的实际问题是为什么
(val: string) => string
不可分配给
(val: string | number) => string | number
那是因为前者不能处理类型的参数number
要检查是否
x
可分配给y
,我们首先查看参数列表。in 中的每个参数都x
必须有对应y
类型兼容的参数 in。
更多关于函数类型兼容性的信息在这里。
因此,一种可能的解决方案是声明可以接受类型
(val: string) => string
函数或类型函数的数组(val: number) => number
const fnArr: (Fn<string> | Fn<number>)[] = []
const fn = (str: string) => str
fnArr.push(fn) // is ok now
推荐阅读
- text-to-speech - 如何以编程方式将阿塞拜疆文本转换为语音?
- c# - 通过给出其索引值来查找列表的对象类型的数据
- c# - 尝试使用 C# 代码在 excel 中将下拉菜单与 if 语句链接起来
- javascript - 合并/替换具有相同 ID 的数组中的对象
- excel - 删除VBA中的部分文本
- python - 尝试在 python-docx 的帮助下访问存储在 aws-s3 存储桶中的 docx 类型文件时遇到问题
- arrays - 在列表 Spring Boot 中设置默认值
- cassandra - 无法运行卡桑德拉
- python - 将 dataframe1 中的单元格值替换为 dataframe2 中先前确定的值
- java - TextView 不会显示为粗体