当我尝试将 concat
与联合类型的数组一起使用时,出现此错误:
This expression is not callable. Each member of the union type '{ (...items: ConcatArray<{...}>[]): { ... }[]; (...items: ({ ... } | ConcatArray<{ ...; }>)[]): { ...; }[]; } | { ...; }' has signatures, but none of those signatures are compatible with each other.
代码沙盒: https://codesandbox.io/s/modern-breeze-qt9mb?file=/src/index.ts
代码示例:
const arr1 = [
{ val1: 1, val2: 2, val3: 3 },
{ val1: 11, val2: 22, val3: 33 }
];
const arr2 = [
{ val1a: "1a", val2a: "2a", val3a: "3a" },
{ val1a: "11a", val2a: "22a", val3a: "33a" }
];
const arr3 = [
{ foo: "lfsfs", bar: "fabgg" },
{ foo: "l414g", bar: "fahrh" }
];
function getRandomArr() {
if (Math.random() < 0.5) {
return arr2;
} else {
return arr1;
}
}
//error
const FinalArr = getRandomArr().concat(arr3);