๐ Search Terms
constant indexed access, array
๐ Version & Regression Information
- This changed between versions
5.4.5 and 5.5.beta
- This changed in commit or PR
โฏ Playground Link
I could not repro this in playground, due to the lib import
๐ป Code
import crypto from 'crypto-js'
const bar = "abc"
const foo: crypto.lib.WordArray = crypto.enc.Utf8.parse(bar) // Returns type: { words: number[], ... }
for (let i = 0; i < foo.words.length; i++) {
foo.words[i] += 1
//~~~~~~~~~~~~ error TS2532: Object is possibly 'undefined'.
// Alternatively:
foo.words[i] = foo.words[i] + 1
// ~~~~~~~~~~~~ <same error>
}
Adding suitable checks in the loop-body for falsey values does not fix it, eg:
if (foo && foo.words && foo.words[i]) {
// Same code as above, same error
}
However the following does fix it:
const word = foo.words[i]
if (word) {
foo.words[i] = word + 1 // OK
}
๐ Actual behavior
error TS2532: Object is possibly 'undefined'.
๐ Expected behavior
No error - this type-checked fine previously
Additional information about the issue
This issue is new since the new Control Flow Narrowing for Constant Indexed Accesses feature. Rolling back to the previous typescript version 'fixes' it
๐ Search Terms
constant indexed access, array
๐ Version & Regression Information
5.4.5and5.5.betaโฏ Playground Link
I could not repro this in playground, due to the lib import
๐ป Code
Adding suitable checks in the loop-body for falsey values does not fix it, eg:
However the following does fix it:
๐ Actual behavior
error TS2532: Object is possibly 'undefined'.
๐ Expected behavior
No error - this type-checked fine previously
Additional information about the issue
This issue is new since the new
Control Flow Narrowing for Constant Indexed Accessesfeature. Rolling back to the previous typescript version 'fixes' it