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 Lind•16mo ago
prov is a list of many prisma row reads
Neto
Neto•16mo ago
that is the type of ProvScores?
Christian Lind
Christian Lind•16mo ago
its a object from prisma or rather its a prisma type
Neto
Neto•16mo ago
let me test here
Christian Lind
Christian Lind•16mo ago
thx!
Neto
Neto•16mo ago
just did this
Neto
Neto•16mo ago
Neto
Neto•16mo ago
and got no issue can you check prov the type on it
Christian Lind
Christian Lind•16mo ago
Christian Lind
Christian Lind•16mo ago
hmm, i think it should be the correct type?
Neto
Neto•16mo ago
not there prov.reduce the type of prov
Christian Lind
Christian Lind•16mo ago
Sorry where do you want me to check the type? where it is defined?
Neto
Neto•16mo ago
hover over the prov variable
Christian Lind
Christian Lind•16mo ago
yeah thats what i did?
Christian Lind
Christian Lind•16mo ago
i just also sent you the definition of ProvScores which is the type of object in Prov 🙂
Neto
Neto•16mo ago
just weird overall did you restart the tsserver?
Christian Lind
Christian Lind•16mo ago
yup
Christian Lind
Christian Lind•16mo ago
also tried to build my next project but get the same error
Christian Lind
Christian Lind•16mo ago
hovered over acc
Neto
Neto•16mo 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 Lind•16mo ago
Christian Lind
Christian Lind•16mo ago
same error, does this code work on your machine?
Neto
Neto•16mo ago
i got no issue on bun let me try another project now i got that issue
Christian Lind
Christian Lind•16mo ago
huh What was the difference between the projects?
Neto
Neto•16mo ago
im checking that rn is some config on tsconfig honestly just do acc[cur.devprov]?.score < cur.score with the ?
Christian Lind
Christian Lind•16mo ago
tried that also...
Christian Lind
Christian Lind•16mo ago
same error here
stanisław
stanisław•16mo 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 Lind•16mo ago
okay so that removed my error - but why would the method that I and @Neto were investigating be incorrect
Neto
Neto•16mo ago
the ts server doing its inference
stanisław
stanisław•16mo 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 Lind•16mo 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•16mo ago
No, you wouldn't get an error if the type was correct
Christian Lind
Christian Lind•16mo 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•16mo 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 Lind•16mo ago
thanks for the help!
Want results from more Discord servers?
Add your server
More Posts