Mr. Robot
Mr. Robot

Reputation: 1824

Object is possibly 'undefined'.ts(2532)

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)}`);
}

Update

Here is the error in the editor:

enter image description here

Upvotes: 1

Views: 1753

Answers (1)

jsw324
jsw324

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

Related Questions