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.
TypeScript automatically infers the type of a value accessed from an array, even if that access is out of bounds. It assumes that the value could be one of the defined types or undefined, which can lead to confusion if you expect stricter enforcement of valid indices.
I just spent the last 2 hours trying to understand why I was getting a valid type from something that shouldn’t have been valid.
I think that the hate that JavaScript receives is well deserved, at least coming from Rust this is an absolute nightmare.
Could be that I misread but I think you’re misunderstanding types in Typescript.
You have an array of 1 or 2 values of unbounded size. You put two values in but could have put 1000 in, your type doesn’t say anything about that. You can only put ones or twos in there though.
I think your first line is equivalent to saying:
type MyStringType = string[];
I haven’t checked though so I might be wrong…