Flux.ai was started around 3 years ago in TypeScript with the default compiler settings. If we could go back in time, there is one setting we would surely change: noUncheckedIndexedAccess. By default, this setting is false. Many people believe it should be true.
What does noUncheckedIndexedAccess do? By default, TypeScript assumes any array element or object property you access dynamically actually exists:
In the example above, the function will throw an error if the string is empty, because str[0] returns undefined and doesn't have a toUpperCase function. TypeScript doesn't warn you about that, regardless of whether strict mode is enabled or not. This is a huge hole in type safety.
The flag noUncheckedIndexedAccess will plug that hole and force you to deal with the possible undefined:
So, why can't we just turn on noUncheckedIndexedAccess? You can, but in a large codebase like that of Flux.ai, you are likely to get thousands of type errors. We had 2761 errors across 373 files! For one speedy engineer converting one file every minute, it would have taken 6+ hours of mind-numbing work to convert all 373 files.
The solution we describe here is how to smoothly convert your codebase with some simple heuristics and automation.
According to Wikipedia, a heuristic technique
is any approach to problem solving or self-discovery that employs a practical method that is not guaranteed to be optimal, perfect, or rational, but is nevertheless sufficient for reaching an immediate, short-term goal or approximation.
That is definitely true here.
The goal was to get the codebase compiling with the new flag, not to fix any bugs. The fixing can come later.
To that end, we intentionally added type assertions ! to suppress all new type errors from undefined types without changing the runtime behavior of the code.
Expanding the scope of replacements to preceding lines allowed us then to automate more fixes with few false positives.
The full script we ran on our codebase is below. Note: it did not fix all the errors. It fixed around 2400 out of 2761 errors, leaving around 100 files for us to fix by hand.
Pro-tip: when experimenting with the replacers and precede, you can simply reset your changes with git reset --hard HEAD (assuming you are working in a git repo).

A guide to flexible PCB design, covering materials, stackups, bend radius, and layout best practices for wearables, medical devices, and other compact electronics.

A beginner-friendly guide to reading PCB schematics, covering common symbols, nets, and how to follow signal flow through a circuit diagram.

An overview of collaborative PCB design, showing how cloud-native tools, real-time editing, and shared libraries are reshaping modern hardware team workflows.

A guide to managing PCB component libraries, covering symbols, footprints, and 3D models with best practices for standardizing parts across hardware teams.

An overview of PCB reverse engineering, explaining how engineers analyze boards, extract schematics, and use the process for legacy support, repair, and design analysis.

A practical guide to PCB silkscreen design, covering labeling best practices, common readability mistakes, and how clean silkscreens improve assembly and debugging.

An explainer on PCB version control, comparing hardware revision workflows to Git-style collaboration and showing how modern teams track design changes.

An introduction to schematic capture, explaining how engineers use symbols, nets, and connectivity to create circuit diagrams that drive PCB layout.