Within my code, I have a function that arranges a set of cards according to a specific coordinate system:
const orderCardsInPage = (items: OrderItemType[], pageCards: CardType[]) => {
return pageCards.sort((firstCard, secondCard) => {
const orderItemOfFirstCard = items.find((orderItem: OrderItemType) => orderItem.card === firstCard._id.toString());
const orderItemOfSecondCard = items.find((orderItem: OrderItemType) => orderItem.card === secondCard._id.toString());
const valueOfFirstOrderItem = orderItemOfFirstCard?.value;
const valueOfSecondOrderItem = orderItemOfSecondCard?.value;
return valueOfFirstOrderItem! - valueOfSecondOrderItem!;
});
};
When attempting to compile the code, an error is thrown at the return
statement due to a missing semicolon error on the non-null assertion symbol (!)
.
Recognizing that the array.find()
function may result in undefined values, I utilized the non-null assertion. How can this issue be resolved?