Reputation: 1824
I am getting this TypeScript error even though I have wrapped it in undefined checks. I just can't seem to be able to get rid of it. message.payload
on the second line of code has the red lines underneath it.
(property) Message.payload?: Payload | undefined Object is possibly 'undefined'.ts(2532)
if (message && message.payload && message.payload.data) {
setDependentPayloads((prev) => `${prev} ${(message.payload.data)}`);
}
Here is the error in the editor:
Upvotes: 1
Views: 1753
Reputation: 812
an exclamation point tells TS you have checked and are certain this element exists.
Try message.payload!
and message.payload!.data!
Upvotes: 7