Typescript bug or am I stupid?

if (pi.latest_charge == null) {
throw new Error('latest_charge is null or string. should have been expanded');
}
const x = pi.latest_charge.id;
const hasDispute = disputes.findIndex((d) => {
return d.charge == pi.latest_charge.id && d.status == 'lost';
}) != -1;
if (pi.latest_charge == null) {
throw new Error('latest_charge is null or string. should have been expanded');
}
const x = pi.latest_charge.id;
const hasDispute = disputes.findIndex((d) => {
return d.charge == pi.latest_charge.id && d.status == 'lost';
}) != -1;
How can pi_latest_charge within the findIndex function be possibly Null? While two lines above, the assignment to x is valid and not possibly null. ps: already restarted lint and typescript server.
No description
3 Replies
cocomo
cocomo11mo ago
That null check is outside the function, If one of the possible values of latest_charge is null then when the function inside findindex is eventually called later, latest_charge might be null. I assume that might be the logic behind the error.
Xanacas
Xanacas11mo ago
Yes, it must be a scoping thing. However, I'm not using findIndex on latest_charge, but on disputes, so pi.latest_charge doesn't change while looking up my disputes.
sincere666
sincere66611mo ago
Yes, it's scoping and garbage collection thing, you need to recheck the pi.latest_charge inside the findIndex()
Want results from more Discord servers?
Add your server