Getting a possibly undefined after undefined check

Getting really bugged with typescript here - what is going on here? The warning is that the object might possibly be undefined
36 Replies
Christian Lind
Christian LindOP•2y ago
prov is a list of many prisma row reads
Neto
Neto•2y ago
that is the type of ProvScores?
Christian Lind
Christian LindOP•2y ago
its a object from prisma or rather its a prisma type
Neto
Neto•2y ago
let me test here
Christian Lind
Christian LindOP•2y ago
thx!
Neto
Neto•2y ago
just did this
Neto
Neto•2y ago
Neto
Neto•2y ago
and got no issue can you check prov the type on it
Christian Lind
Christian LindOP•2y ago
Christian Lind
Christian LindOP•2y ago
hmm, i think it should be the correct type?
Neto
Neto•2y ago
not there prov.reduce the type of prov
Christian Lind
Christian LindOP•2y ago
Sorry where do you want me to check the type? where it is defined?
Neto
Neto•2y ago
hover over the prov variable
Christian Lind
Christian LindOP•2y ago
yeah thats what i did?
Christian Lind
Christian LindOP•2y ago
i just also sent you the definition of ProvScores which is the type of object in Prov 🙂
Neto
Neto•2y ago
just weird overall did you restart the tsserver?
Christian Lind
Christian LindOP•2y ago
yup
Christian Lind
Christian LindOP•2y ago
also tried to build my next project but get the same error
Christian Lind
Christian LindOP•2y ago
hovered over acc
Neto
Neto•2y ago
just weird overall from tsserver paste this code where would be old ones just to be sure
const highestScore = prov.reduce((acc, cur) => {
if (acc[cur.delprov]) {
if (acc[cur.delprov].score < cur.score) {
acc[cur.delprov] = cur;
}
} else {
acc[cur.delprov] = cur
}

return acc
}, {} as Record<number, ProvScores>);
const highestScore = prov.reduce((acc, cur) => {
if (acc[cur.delprov]) {
if (acc[cur.delprov].score < cur.score) {
acc[cur.delprov] = cur;
}
} else {
acc[cur.delprov] = cur
}

return acc
}, {} as Record<number, ProvScores>);
Christian Lind
Christian LindOP•2y ago
Christian Lind
Christian LindOP•2y ago
same error, does this code work on your machine?
Neto
Neto•2y ago
i got no issue on bun let me try another project now i got that issue
Christian Lind
Christian LindOP•2y ago
huh What was the difference between the projects?
Neto
Neto•2y ago
im checking that rn is some config on tsconfig honestly just do acc[cur.devprov]?.score < cur.score with the ?
Christian Lind
Christian LindOP•2y ago
tried that also...
Christian Lind
Christian LindOP•2y ago
same error here
stanisław
stanisław•2y ago
guys the error is correct To fix it you'd have to make something like this
const currValue = acc[cur.delprov]

if(currValue){
if(currValue.score < cur.score)
}
const currValue = acc[cur.delprov]

if(currValue){
if(currValue.score < cur.score)
}
if u assign it to a variable then ts will know its not undefiend after the check
Christian Lind
Christian LindOP•2y ago
okay so that removed my error - but why would the method that I and @Neto were investigating be incorrect
Neto
Neto•2y ago
the ts server doing its inference
stanisław
stanisław•2y ago
becuase after you check an object property like this acc[cur.delprov] ts has no insurance that you are not mutating an object afterwards once you assign it then ts knows "currValue is x"
Christian Lind
Christian LindOP•2y ago
ah okay, so if i would have instead said let currValue = ..., then that would also give an error since then the compiler can't know if the variable has changed value
stanisław
stanisław•2y ago
No, you wouldn't get an error if the type was correct
Christian Lind
Christian LindOP•2y ago
but if it is a mutable variable i could reassign it to be undefined, no? Or is that when the type error would kick in
Neto
Neto•2y ago
the let only implies that you can change the value using let currVal = null then you have some issues with inferring the type
Christian Lind
Christian LindOP•2y ago
thanks for the help!
Want results from more Discord servers?
Add your server