-
Notifications
You must be signed in to change notification settings - Fork 12.8k
noUncheckedIndexedAccess should be enabled by default in strict mode #49169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's intentionally not part of |
In general when we add a new flag that adds new errors, the decision point for putting it in This behavior is unambiguously in the latter camp on account of breaking every C-style loop in existence. |
@RyanCavanaugh Would you consider changing it in a major version like TS 5.0? I think we have plenty of loop options today: for...of, forEach, etc. And C-style loops suffer from off-by-one errors. So I think it would make sense to enable |
Even if we decided to change some defaults in 5.0, which is certainly on the table, I don't think this one meets the bar. There are too many common sound-by-construction idioms that cause false positives with it. But this is probably the one that comes closest to wanting to be on by default. If we end up with a very robust configuration versioning story where there was an unambiguous opt-in to "latest and greatest as of right now", it might make sense there. |
This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
fun fact: I totally forgot about this ticket. Just today faced a similar situation (but I likely remembered about the flag and enabled it) Here is how I think about it as a user: when I enable the strict flag, I expect it to be, well ..., strict 😅 I think what we are misaligned on is how bad are those "false positives". Can someone kindly tell me some examples of how annoying such cases be? |
I would say we get 1-2 reports a week of "noUncheckedIndexedAccess is not smart enough". A sampling: #55206 |
Related to this, I'm wondering if there's a reason why In my opinion it would also be nice to add a non-null assertion code example there, as it is a useful escape hatch: interface EnvironmentVars {
NAME: string;
OS: string;
// Unknown properties are covered by this index signature.
[propName: string]: string;
}
declare const env: EnvironmentVars;
// Declared as existing
const sysName = env.NAME;
const os = env.OS;
// ^? const os: string
// Not declared, but because of the index
// signature, then it is considered a string
const nodeEnv = env.NODE_ENV;
// ^? const nodeEnv: string | undefined
+ // The non-null assertion operator (!) can
+ // still be used for unchecked index access
+ const nodeEnvString = env.NODE_ENV!;
// ^? const nodeEnvString: string |
The previous comment is why |
I agree that enabling the But in my experience, Yeah, some "false positives" do occur but the non-null assertion operator makes that a trivial issue if we can't write safer code. To illustrate this in e.g. #51988 that you mentioned: const numbers = [1, 2, 3]; // Let's assume we only know this has type number[]
if (numbers.length > 0) {
const first = numbers[0]!; // Non-null assertion, explicitly opting out of type safety
const doubleFirst = first * 2;
}
// Better in this case to not rely on Array: length
const first = numbers[0];
if (first) {
const doubleFirst = first * 2;
} |
because even `strict: true` in `tsconfig.json` does not enable it - see microsoft/TypeScript#49169
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 aboutnoUncheckedIndexedAccess
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:
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
type of test is
number
and notnumber | 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 strictThe text was updated successfully, but these errors were encountered: