TS Unchecked Indexed Access
Hey guys, I have a question about ts. I have a "noUncheckedIndexedAccess": true in my tsconfig.
I am trying to make a function (well, chatgpt helped me create a function lol) that is a calculator to get Levenshtein distance (basically for a fuzzy search, but it's not the main point).
as a part of it I have a for loop along the lines of this
So I get the error saying
res[i] Object is possibly undefined. - on line
So my question is this: is there a way for compile to get it correctly as I literally define res[i] on the line before? or is it some sort of a bug on my part? or is it something not possible?
P.S.
if you are interested here's the full function
and I know I can cast types, but I want to do it without 'as ...'
3 Replies
bump
since res[i] is an object you can create another reference to it and check it with an if statement like
But I would recommend just pushing the values to the array instead of setting the items.
thanks, that's what I kinda thought, just make a new reference and check it