In TypeScript, if strict null checking is enabled, I would expect the compiler to prevent me from assigning null
or undefined
values to a variable unless it admits null
.
However, array access seems to allow circumventing this check.
Example:
let a: string[] = ["Hello"];
let s: string;
// 1) this produces an error, as expected
s = undefined
// 2) s is undefined here, too, but no error
s = a[3];
console.log(s);
Runnable version on the TypeScript Playground (Note: "strict null checking" must be enabled in the "Options" dialog).
What is going on here?
- Is this a bug in the TypeScript compiler?
- Or is it a deliberate omission?
- If the latter, is this documented anywhere (ideally with a rationale why it was done) ?