You have two options depending on how you set your Typescript config.
Option 1, the default:
declare const xs: number[]
const x = xs[4] // inferred type is `number`
Option 2, using the noUncheckedIndexedAccess setting:
declare const xs: number[]
const x = xs[4] // inferred type is `number | undefined`
Your AI assistant appears to assume option 2. Maybe you have that option enabled in your project?
I'm sorry you had to spend a lot of time and frustration on this problem. But fundamentally Rust and Typescript have the same limitation: neither will catch out-of-bounds access errors on variable-length collections at type-checking time. They don't have the necessary information to do that.
Rust can catch out-of-bounds access on a fixed-length array if you use a literal number for the index access. But Typescript can do the same thing if you use a fixed-length tuple type (e.g. [number, number] instead of number[]).
TypeScript does not throw an error at compile time for accessing an out-of-bounds index. Instead, it assumes that the value could be one of the types defined in the array (in this case, 1 or 2) or undefined.