Closed as not planned
Description
π Search Terms
constant indexed access, array
π Version & Regression Information
- This changed between versions
5.4.5
and5.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