-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Array access possibly undefined with noUncheckedIndexedAccess despite findIndex #61388
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
This is working as intended. |
Well, of course it doesn't narrow the arr, but it guarantees that |
If you consider this too complex, there's also the simpler case of const arr: number[] = [1, 2, 3];
const idx: number = ...;
if (idx >= 0 && idx < arr.length) {
const x: number = arr[idx];
console.log(x);
} which fails with the same error. |
It's just a limitation of how TypeScript is designed. It needs to narrow the type, otherwise the check does not work. There's no hardcoded special behavior for I suggest you to write the code this way: const arr: number[] = [1, 2, 3];
const idx: number = ...;
const x: number | undefined = arr[idx];
if (x !== undefined) {
// ...
} |
What I'm saying is that there's guarantees about the range of I don't think it's that much different from narrowing a range of possible string values, e.g. like here: type Rarity = "COMMON" | "UNCOMMON" | "RARE";
const probabilities: Record<Exclude<Rarity, "RARE">, number> = { COMMON: 80, UNCOMMON: 20 };
declare function getKey(): Rarity;
const key: Rarity = getKey();
if (key != "RARE") {
const probability: number = probabilities[key];
console.log(probability);
} |
Completely different case. Here the
And I'm telling you: The compiler doesn't know about this guarantee. There's nothing about the signature of |
Well, in the case of |
This can't be done either. You need to think about it on a type level, not on a runtime level. You see the code and think yourself "yeah, of course this will be fine when running", but how would the compiler know about this by looking at the types? |
I would recommend turning off Not directed at OP: I kept trying to warn people this would happen π« |
FWIW, I personally am only looking into adoption of this flag to reduce the number of false-positives I'm getting from eslint's |
Apologies for the drive-by nitpicking, but: we do have interest! We're just blocked from doing anything ourselves. The situation is that we directly use TypeScript's inference. Anything like the requested "do this one specific case better" would need to be solved at the TypeScript level. Asking typescript-eslint to do better here is like asking your coffee mug to give you fresher coffee beans. It's a delivery method, not the roaster itself. π |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
noUncheckedIndexedAccess, findIndex, undefined
π Version & Regression Information
Tried 5.8.2 and nightly (5.9.0-dev.20250310).
β― Playground Link
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.8.2#code/MYewdgzgLgBAhgJwQLhmArgWwEYFMEDaAujALwwECMANDAEy0DMRA3AFCiSwCWAJgB5l4SAHQAzbmF4BJKbn4AKQaQB8MZeToBKdtzEwFfQQEJyAWkpaYAbzYx7MTtHWoMOfEMSEjrOw6cgADa4IoEgAOZKOmwAvmxAA
π» Code
π Actual behavior
π Expected behavior
Code 'compiles' without errors.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: