Below is a snippet of sample code:
interface Foo {
bar: string;
buz: string;
}
const fooList: Foo[] = [
{ bar: '1', buz: '1' },
{ bar: '2', buz: '2' }
];
const newList: Foo[] = fooList.map((foo: Foo) => ({
bar: foo.bar,
}));
Upon execution, an error message
Property 'buz' is missing in type '{ bar: string; }'
will be displayed.
If I modify the type of fooList
to any
, TypeScript does not show any errors:
interface Foo {
bar: string;
buz: string;
}
const fooList: any = [
{ bar: '1', buz: '1' },
{ bar: '2', buz: '2' }
];
const newList: Foo[] = fooList.map((foo: Foo) => ({
bar: foo.bar,
}));
In this scenario, I anticipated receiving the same error as before. Why does TypeScript not raise an error in the second case?