Consider this TypeScript-code:
export class Lala {
id: number = Infinity;
arr: { [key: number]: string } = {};
get(nr: number): string {
return this.arr[nr];
}
}
const lala = new Lala();
const x: string = lala.get(555);
For my understanding the function Signature get(nr: number): string
and the assignment of x should throw an TypeScript error, because the value can be undefined if an index was given which did not exist in the array. It doesn’t, even if strictNullChecks is enabled (which seemed to address the exact issue) and I get exactly the type of runtime errors TypeScript is created to avoid.
Why is the given code not raising an error and is there some switch for doing so?
1