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
prov is a list of many prisma row reads
that is the type of ProvScores?
its a object from prisma
or rather its a prisma type
let me test here
thx!
just did this
and got no issue
can you check prov
the type on it
hmm, i think it should be the correct type?
not there
prov.reduce
the type of prov
Sorry where do you want me to check the type?
where it is defined?
hover over the prov variable
yeah thats what i did?
i just also sent you the definition of ProvScores which is the type of object in Prov 🙂
just weird overall
did you restart the tsserver?
yup
also tried to build my next project but get the same error
hovered over acc
just weird overall from tsserver
paste this code where would be old ones
just to be sure
same error, does this code work on your machine?
i got no issue on bun
let me try another project
now i got that issue
huh
What was the difference between the projects?
im checking that rn
is some config on tsconfig
honestly
just do
acc[cur.devprov]?.score < cur.score
with the ?tried that also...
same error here
guys the error is correct
To fix it you'd have to make something like this
if u assign it to a variable then ts will know its not undefiend after the check
okay so that removed my error - but why would the method that I and @Neto were investigating be incorrect
the ts server doing its inference
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"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 valueNo, you wouldn't get an error if the type was correct
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
the let only implies that you can change the value
using let currVal = null
then you have some issues with inferring the type
thanks for the help!