object might be undefined, but have a undefined check

Here is my code, and im getting a "Object is possibly 'undefined'.ts(2532)" error. messagesWithCoach.data is a json object with the key being a coachId (String), and the value a list of objects Why am i still getting this error even though im checking if its undefined
8 Replies
Brendonovich
Brendonovich17mo ago
i think the problem is you're using typeof typeof anything will always give you a string, so typeof ... !== undefined will always be true and won't make typescript do type narrowing either put the undefined in quotes or remove the typeof and it may work
Christian Lind
Christian Lind17mo ago
alright, lets check and see!
Christian Lind
Christian Lind17mo ago
error is persisting
Brendonovich
Brendonovich17mo ago
it may also just be the case that ts can't type narrow for an expression that complex
Matvey
Matvey17mo ago
messagesWithCoach.data?.[coachId]?.map()
messagesWithCoach.data?.[coachId]?.map()
Brendonovich
Brendonovich17mo ago
but yeah u can just nullish coalesce before calliing map
Christian Lind
Christian Lind17mo ago
genius! Thanks for the help, feeling fairly dumb now haha
Matvey
Matvey17mo ago
No problem t3ggLove
Want results from more Discord servers?
Add your server