数组的打字稿联合和联合数组

时间:2020-01-16 13:08:09

标签: typescript union-types

我已经定义了一些这样的类型:

type Type1 = {
    prop1: string,
}
type Type2 = {
    prop2: string,
}

let x1: (Type1 | Type2)[] = [];
x1.map(y => y); // OK

let x2: Type1[] | Type2[] = [];
x2.map(y => y); // ERROR

当我尝试在x1上映射时,一切都很好,但在x2上映射却无法正常工作,并显示一个错误:

该表达式不可调用。

如何将Type1[] | Type2[]转换为(Type1 | Type2)[]

0 个答案:

没有答案