Check out this demonstration I made in the TypeScript playground:
interface Test{
a: string
b: string
}
const object: Test = {
a: 'b',
b: 'c',
}
function testIt(): Test[] {
const data = [{b: '2', c: '3'}]
const prices: Test[] = data.length ? data.map((item) => {
return {
a: item.b,
b: item.c,
c: '2',
d: '3'
}
}) : [];
return prices;
}
If you remove either the 'a' or 'b' property from the object return statement within the array map method, it will result in a TypeScript error (as expected).
However, adding properties like 'c' or 'd' or any other unknown property does not trigger a TypeScript error. This behavior may only occur if the interface includes [x: string]: any.
So, why does Array.map perform type checking on missing properties in an interface but not on additional/unknown properties?