【发布时间】:2019-11-16 00:21:57
【问题描述】:
我只是想知道为什么智能感知的类型推断会在 Array.isArray 条件中丢失。
考虑以下 sn-p:
type T = {
readonly name: string;
readonly descr: string;
}
interface I{
readonly tags: ReadonlyArray<T>;
}
function Z(arg: I): void{
const { tags } = arg;
if (Array.isArray(tags)) { //hovering "tags" here shows "readonly T[]"
for (let t of tags) { //hovering "tags" here shows "any[]"
}
}
}
Z({
tags:[]
})
换句话说,为什么原始类型没有从其声明中保留下来,而是通过isArray 签名进行更改?
在 Visual Studio 以及 playground 中测试。
【问题讨论】:
标签: typescript