【发布时间】:2017-05-07 03:43:47
【问题描述】:
我有 3 个接口(下面的A、B 和C)都从一个通用接口(下面的Common)扩展而来。我还有一个容器类型,其中包含这 3 个接口的数组(下面的Container)。
我想抓取其中一个数组并从对象中提取一个公共属性,如 getCommon()` 所示:
interface Common {
common: boolean;
}
interface A extends Common {
a: A[];
}
interface B extends Common {
b: B[];
}
interface C extends Common {
c: C[];
}
type Container = {
a: A[],
b: B[],
c: C[]
};
let item: Container = {
a: [ { a: [], common: true } ],
b: [ { b: [], common: true } ],
c: [ { c: [], common: true } ]
};
type Key = 'a' | 'b' | 'c';
function getCommon (thisKey: Key) {
return item[thisKey].map(a => a.common); // This line has an error
}
但是,Typescript 2.1 给了我一个编译错误:
Cannot invoke an expression whose type lacks a call signature.
Type '{ <U>(this: [A, A, A, A, A], callbackfn: (value: A, index: number, array: A[]) => U,
thisArg?: an...' has no compatible call signatures.
这是 Typescript 中的错误/限制,还是我做错了什么?
【问题讨论】: