I found a very surprising behavior of Typescript's type-inference and suspect if it is a bug.
Suppose I have a list of MyItem interface.
interface MyItem {
id?: string;
value: string;
}
const myItemList: MyItem[] = [];
Please notice that the MyItem.id is optional while MyItem.value is mandatory.
I want to use method Array.reduce on myList, like this
const myMap = myItemList.reduce((accum, item) => ({ ...accum, [item.id!]: item }), {});
This will make the result variable myMap to be of type {}, which is as expected because I supplies {} as the initial value in myItemList.reduce(...), as shown in the picture below.
Now, the problem occurs when I modify the MyItem interface so that the value becomes optional.
interface MyItem {
id?: string;
value?: string; // << here I make it optional
}
const myItemList: MyItem[] = [];
Surprisingly, the exact same myItemList.reduce(...) statement yields a result of MyItem instead of {}.
Is this a bug? If not, please help me understand what is the reason behind it.

