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
Brendonovich2y 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
alright, lets check and see!
Christian Lind
error is persisting
Brendonovich
Brendonovich2y ago
it may also just be the case that ts can't type narrow for an expression that complex
Matvey
Matvey2y ago
messagesWithCoach.data?.[coachId]?.map()
messagesWithCoach.data?.[coachId]?.map()
Brendonovich
Brendonovich2y ago
but yeah u can just nullish coalesce before calliing map
Christian Lind
genius! Thanks for the help, feeling fairly dumb now haha
Matvey
Matvey2y ago
No problem t3ggLove
Want results from more Discord servers?
Add your server