the error is currEmot is possibly undefined. I'm confused why that is occurring because the for loop is guaranteed to loop through the emotionsObject and add objects to the map.
let emotionsObject = [
{
size: 80,
background: 'rgb(67, 0, 128)',
width: .5,
emoji: "😄",
name: 'joy'
},
{
size: 34,
background: 'rgb(0, 45, 172)',
width: .35,
emoji: "😭",
name: 'sadness',
},
{
size: 50,
background: 'rgb(172, 6, 0)',
width: .15,
emoji: "😡",
name: 'anger',
},
];
type T_Emotion = typeof emotionsObject[0]
let currEmot = new Map<string, T_Emotion>(null!);
let emotOrder: string[] = [];
for (let i = 0; i < emotionsObject.length; i++) {
let id = uuid_v4();
emotOrder.push(id)
currEmot.set(id, emotionsObject[I]);
}
emotOrder.sort((a: string, b: string):number => currEmot.get(b).width - currEmot.get(a).width);
I have tried these other versions to prevent this error.
emotOrder.sort((a: string, b: string):number => currEmot?.get(b)?.width - currEmot?.get(a)?.width);
emotOrder.sort((a: string, b: string):number => currEmot.get(b).width as number - currEmot.get(a).width as number);
null!...huh?Icome from all of the sudden?