Skip to content
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

Indexing an array by a number should be an optional return value #2120

Open
2 tasks
Gouvernathor opened this issue Nov 24, 2024 · 1 comment
Open
2 tasks
Labels

Comments

@Gouvernathor
Copy link

🐞 bug report

Is this a regression?

No idea.

Description

The indexing of a T[] by a number is typed as a T, instead of a T|undefined.
I encountered that when applying a .filter to one of my arrays then doing [0].fieldaccess, and then noticing no error despite that field access failing if the filter returns an empty array (since the 0-indexing returns undefined).

Bug Type

What does this bug affect

  • Angular Language Service VSCode extension
  • Angular Language Service server

(not sure which, if any)

Reproduction

function f(a: number[]) {
  const b = a[0];
  b.toFixed(0); // no typing error report
}

Hovering b shows it as type number, when it should be number|undefined.

Logs

No log since this has no effect on performance and nothing is reported when it should.

Screenshots

Image

🌍 Your Environment

Angular Version:


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 18.2.12
Node: 22.11.0
Package Manager: npm 10.9.1
OS: win32 x64

Angular: 18.2.12
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1802.12
@angular-devkit/build-angular   18.2.12
@angular-devkit/core            18.2.12
@angular-devkit/schematics      18.2.12
@schematics/angular             18.2.12
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.14.10

Extension Version:


v19.0.2

VSCode Version:


Version: 1.95.3 (system setup)
Commit: f1a4fb101478ce6ec82fe9627c43efbf9e98c813
Date: 2024-11-13T14:50:04.152Z
Electron: 32.2.1
ElectronBuildId: 10427718
Chromium: 128.0.6613.186
Node.js: 20.18.0
V8: 12.8.374.38-electron.0
OS: Windows_NT x64 10.0.19045

Operating System:


Win10

Extension options:
I didn't find any settings.json file.

@Khyalis
Copy link

Khyalis commented Dec 30, 2024

Might the noUncheckedIndexedAccess typescript compiler option help with the issue?

The doc example may not be super obvious here, but this option will cause undefined to be included in the return type for index read access operations, as with the array in question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants