Enable `noUncheckedIndexedAccess` TypeScript option
Problem
TypeScript does not warn when you access an array or object by index. This means code like this compiles and passes all checks without any complaint:
const items = getKernelList();
const first = items[0];
first.execute(); // crashes silently at runtime if the list is emptyThis affects real runtime boundaries throughout the codebase such as:
- Array index access where the index may be out of bounds
- Object and dictionary map lookups where the key may not exist
- Buffer reads at positions that may be empty
- String
split()results where a part may be missing - Registry dictionaries where an entry may not be registered
- Selection lists where nothing may be selected
Findings from Testing
To understand the scope before proposing this, I ran a dry run using
no-emit TypeScript checks, comparing the baseline build against a build
with --noUncheckedIndexedAccess enabled temporarily.
Enabling this option produced the following.
Diagnostics:
| Area | diagnostics | Files |
|---|---|---|
| packages/* source | 941 | 154 |
| buildutils | 69 | 12 |
| galata | 10 | 4 |
| examples | 4 | 2 |
| Total | 1,024 | 172 |
The affected packages:
| Package | errors |
|---|---|
| notebook | 165 |
| filebrowser | 80 |
| codemirror | 52 |
| ui-components | 48 |
| rendermime | 47 |
| settingregistry | 47 |
| services | 41 |
Proposed Solution
We can enable noUncheckedIndexedAccess gradually, adding no-unsafe-member-access wouldnt add much since it will have nearly the same effect as the #18854
For each package the process could be:
- Enable noUncheckedIndexedAccess: true in that package's own tsconfig.json
- Fix all violations within that package with proper guards or assertions
- Open a focused PR for just that package
- Move to the next package
References
https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess
Source: jupyterlab/jupyterlab