In this code snippet, I am encountering an issue with the TypeScript compiler where it only raises an error for getThings_nocats
but not for getThings_concat
:
interface IThing {
name: string;
}
function things1() {
return [
{name: 'bob'},
{name: 'sal'},
]
}
function things2() {
return [
{garbage: 'man'},
]
}
function getThings_concat():Array<IThing> {
return <Array<IThing>>([].concat(things1(), things2()));
}
function getThings_nocats():Array<IThing> {
let ret:Array<IThing> = [];
things1().forEach(thing => {
ret.push(thing);
});
things2().forEach(thing => {
ret.push(thing);
});
return ret;
}
Although I receive only one compiler error currently, ideally I would like to have two errors, one for each of the problematic functions:
test.ts(24,18): error TS2345: Argument of type '{ garbage: string; }' is not assignable to parameter of type 'IThing'.
Property 'name' is missing in type '{ garbage: string; }'.
I am looking for a solution to modify getThings_concat
so that it can still use [].concat
without causing errors when things2()
returns non-IThing
objects. How can I achieve this?