Why does indexing in an array break type safety in TypeScript?

Use of the index operator on an object with arbitrary key returns type any, which can be assigned to any type without causing type errors. However, this can be resolved by using the --noImplicitAny compiler option.

Yes. Use the noImplicitAny, if you care for strict safety. Also strict:true (which strictNullChecks as well as others).

My question is why does something as basic as indexing on an array break type safety? Is this a design constraint, an oversight, or a deliberate part of TypeScript?

There are levels of safety. strict is the strongest. You choose how strict you want to be with your code.

More

From https://basarat.gitbooks.io/typescript/content/docs/options/intro.html

That said, traditionally programming languages have a hard boundary between what is and isn't allowed by the type system. TypeScript is different in that it gives you control on where you put the slider.


To answer the why part of your question, it is because the developers of TypeScript thought that it would be too uncomfortable if you always had to check for undefined results when indexing an Array.

For example, the following code would not type-check:

var arr: number[]
var s = 0
for (var i in arr) {
  s += arr[i]
}

Furthermore, because JavaScript Arrays can be sparse, even if the index is known to not be out of bounds (as in, >= 0 and < arr.length), you can still get undefined, so there seems to be no way to fully infer actual unsafe indexing.

For more on this, see the issues TypeScript#13778 and TypeScript#11122.