Comments from @jonrsharpe and @shinigami helped me understand this! Paraphrasing:
Arrays are basically
{ [index: number]: T }
, where every value ofindex
is assumed to have a definedT
value. One can use a tuple type if a specific length is expected.But with the flag
--noUncheckedIndexedAccess
(to be included in TypeScript 4.1), the behavior will match my original expectation. In particular:
Under this new mode, every property access (like foo.bar) or indexed access (like foo[“bar”]) is considered potentially undefined.
CLICK HERE to find out more related problems solutions.