Description
Bug Report
const numbers: Array = [];
// this should fail, type of test should be number | undefined
const test = numbers[0].toFixed();
I was surprised that this is valid in strict
mode. started writing issue about that then meanwhile found about noUncheckedIndexedAccess
so now I'm changing the issue to enable it by default and make thousands of codebases safer, hopefully :D
🔎 Search Terms
"original" issue about unsafe array access:
array index undefined
after my realization:
noUncheckedIndexedAccess
🕗 Version & Regression Information
I think since ever, I did check the list anyway
"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs
If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: typescript@next
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly
Note: The TypeScript Playground can be used to try older versions of TypeScript.
Please keep and fill in the line that best applies:
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about common bugs
⏯ Playground Link
Playground link with relevant code
💻 Code
const numbers: Array<number> = [];
const test = numbers[1] // should be number | undefined instead of number
🙁 Actual behavior
type of test is number
and not number | undefined
🙂 Expected behavior
After a lot of research I found out about noUncheckedIndexedAccess
which is great but would be awesome if it's enabled by default for strict