There are 3 interfaces (A
, B
, and C
) that all extend from a shared interface (Common
). Additionally, there is a container type which holds arrays of these 3 interfaces (Container
).
The goal is to select one of the arrays and extract a common property from the objects. This can be achieved by using the getCommon() function:
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); // There seems to be an error in this line
}
If you want to play around with this code snippet, feel free to do so on the Typescript Playground link
Despite following the correct syntax, Typescript 2.1 is generating a compilation error:
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.
It's unclear whether this issue is a bug or limitation within Typescript itself, or if there might be something wrong in the implementation.