-
Notifications
You must be signed in to change notification settings - Fork 12.8k
noUncheckedIndexedAccess does not compile on incorrect possible undefined #51988
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
@MartinJohns it seems redundant having check the first element for |
Regardless, I would agree the error should not throw here since |
@WesleyYue are you sure that the array can contain const numbers: number[] = [1, 2, 3, undefined]; Even if I do not specify the type on the array the result is the same. const numbers = [1, 2, 3];
numbers.push(undefined) |
@WesleyYue I mean yes if the array was not clearly defined, but as you say the array is clearly defined. The error should not be thrown so we agree on that |
Checking the |
For the record, it's entirely possible for this to happen at runtime: let arr: number[] = [];
arr[1] = 42;
console.log(arr.length); // 2 (i.e. arr.length > 0)
console.log(arr[0]); // undefined! It's debatable whether most code needs to be hardened against that (sparse arrays are evil), but seems worth considering, at least. |
Bug Report
π Search Terms
noUncheckedIndexedAccess
π Version & Regression Information
noUncheckedIndexedAccess: true
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The code does not compile since the compiler thinks
first
can beundefined
.π Expected behavior
The compiler should notice that
first
must be of typenumber
and notnumber | undefined
since the if statement ensures that.Of course the code compiles if we disable
noUncheckedIndexedAccess
, but I would like to keep it enabled.The text was updated successfully, but these errors were encountered: