-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Description
Update: fixed by --noUncheckedIndexedAccess in TypeScript 4.1
Update: for my latest proposal see comment #13778 (comment)
With strictNullChecks enabled, TypeScript does not include undefined in index signatures (e.g. on an object or array). This is a well known caveat and discussed in several issues, namely #9235, #13161, #12287, and #7140 (comment).
Example:
const xs: number[] = [1,2,3];
xs[100] // number, even with strictNullChecksHowever, it appears from reading the above issues that many TypeScript users wish this wasn't the case. Granted, if index signatures did include undefined, code will likely require much more guarding, but—for some—this is an acceptable trade off for increased type safety.
Example of index signatures including undefined:
const xs: number[] = [1,2,3];
xs[100] // number | undefinedI would like to know whether this behaviour could be considered as an extra compiler option on top of strictNullChecks. This way, we are able to satisfy all groups of users: those who want strict null checks with or without undefined in their index signatures.