Introduction

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.

The flag

What does noUncheckedIndexedAccess do? By default, TypeScript assumes any array element or object property you access dynamically actually exists:

function uppercaseFirst(str: string) {
  return str[0].toUpperCase() + str.slice(1);
}

uppercaseFirst('') // runtime error BAD!!!

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:

function uppercaseFirst(str: string) {
  return str[0]?.toUpperCase() + str.slice(1); // note: ? nullish operator
}

uppercaseFirst('') // returns ''

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.

Heuristics

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.

const firstLetter = str[0] // needs too be str[0]!
const rest = str.slice(1)
const upperFirst = firstLetter.toUpperCase()

Expanding the scope of replacements to preceding lines allowed us then to automate more fixes with few false positives.

Automation

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).

#!/usr/bin/env ts-node

// To generate noUncheckedIndexedAccess.txt, run
// $ npx tsc | grep 'error T' > noUncheckedIndexedAccess.txt

import {readFileSync, writeFileSync} from "fs";

type ErrorLines = {path: string; lineNum: number; message: string}[];

// NOTE: these should be idempotent for safety!
const replacers: [RegExp, string][] = [
    [/(\w+\.\w+\.\w+)\.(\w+)/g, "$1!.$2"], // a.b.c.d to a.b.c!.d
    [/(\w+\[(\w|\.)+\])!*/g, "$1!"], // add ! after []
    [/(\w+\])(\[\w+\])/g, "$1!$2"], // add ! between [][]
    [/(\[\w+\])(\.\w+)/g, "$1!$2"], // add ! between [] and .
    [/(\[\d\]?)!*/g, "$1!"], // add ! after [0]
    // START CORRECTIONS
    [/\]!\) =>/g, "]) =>"], // correcting add ! above
    [/\]! =/g, "] ="], // correcting add ! above
];

const precede = 2;

function main() {
    const txt = readFileSync("./noUncheckedIndexedAccess.txt", "utf-8");
    const errorLines = parseErrorLines(txt);
    errorLines.forEach((errorLine) => {
        let lineText = readLine("../" + errorLine.path, errorLine.lineNum, precede) as string;
        replacers.forEach(([match, replacement]) => {
            const newLineText = getNewLineText(lineText, match, replacement);
            if (newLineText) lineText = newLineText;
        });
        console.log("\n---");
        console.log(errorLine.path, errorLine.lineNum, "\n", lineText);
        console.log("---\n");
        writeLine("../" + errorLine.path, errorLine.lineNum, lineText, precede);
    });
}

function getNewLineText(lineText: string, match: RegExp, replacement: string) {
    return (
        lineText
            .split("\n")
            // @ts-ignore: ignore missing string method
            .map((line) => line.replaceAll(match, replacement))
            .join("\n")
    );
}

function parseErrorLines(txt: string): ErrorLines {
    return txt
        .split("\n")
        .filter(Boolean)
        .map((line) => {
            const [pathPlus, message] = line.split(": error ");
            const pieces = pathPlus?.split("(");
            if (!pieces || !pieces[0] || !pieces[1] || !message) {
                throw new Error(`Missing bits in line: ${line}`);
            }
            const numberPieces = pieces[1].split(",", 1);
            if (!numberPieces || !numberPieces[0]) {
                throw new Error(`Missing numbers in pieces: ${pieces}`);
            }
            const lineNum = parseInt(numberPieces[0], 10);
            if (!(lineNum > 0 && lineNum < 1000000)) {
                throw new Error(`Bad line number: ${lineNum}`);
            }
            return {
                path: pieces[0],
                lineNum,
                message,
            };
        });
}

function readLine(filename: string, lineNum: number, precede: number) {
    const lines = readFileSync(filename, "utf8").split("\n");
    return lines.slice(lineNum - 1 - precede, lineNum).join("\n");
}

function writeLine(filename: string, lineNum: number, lineText: string, precede: number) {
    const lines = readFileSync(filename, "utf8").split("\n");
    lines.splice(lineNum - 1 - precede, precede + 1, ...lineText.split("\n"));
    writeFileSync(filename, lines.join("\n"));
}

main();
Profile avatar of the blog author

Greg Dingle

Building the future with friends

Go 10x faster from idea to PCB
Work with Flux like an engineering intern—automating the grunt work, learning your standards, explaining its decisions, and checking in for feedback at key moments.
Illustration of sub-layout. Several groups of parts and traces hover above a layout.
Design PCBs with AI
Introducing a new way to work: Give Flux a job and it plans, explains, and executes workflows inside a full browser-based eCAD you can edit anytime.
Screenshot of the Flux app showing a PCB in 3D mode with collaborative cursors, a comment thread pinned on the canvas, and live pricing and availability for a part on the board.
Design PCBs with AI
Introducing a new way to work: Give Flux a job and it plans, explains, and executes workflows inside a full browser-based eCAD you can edit anytime.
Screenshot of the Flux app showing a PCB in 3D mode with collaborative cursors, a comment thread pinned on the canvas, and live pricing and availability for a part on the board.
Design PCBs with AI
Introducing a new way to work: Give Flux a job and it plans, explains, and executes workflows inside a full browser-based eCAD you can edit anytime.
Screenshot of the Flux app showing a PCB in 3D mode with collaborative cursors, a comment thread pinned on the canvas, and live pricing and availability for a part on the board.

Related Content

PCB Prototyping vs. Fabrication: Which Process Is Right for Your Project?

PCB Prototyping vs. Fabrication: Which Process Is Right for Your Project?

A practical guide to when hardware teams should use low-volume PCB prototyping to validate a design versus full-scale fabrication to scale production, and how to transition between the two without costly mistakes.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|April 21, 2026
Blind Vias, Buried Vias, and Microvias: A Complete Guide to PCB Via Types

Blind Vias, Buried Vias, and Microvias: A Complete Guide to PCB Via Types

A practical guide to the four main PCB via types — through-hole, blind, buried, and microvia — covering how each is fabricated, their cost and signal-integrity trade-offs, and when to use them based on layer count, BGA pitch, and routing density.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|April 21, 2026
PCB Design for Manufacturability (DFM): Rules and Best Practices

PCB Design for Manufacturability (DFM): Rules and Best Practices

Learn PCB design for manufacturability (DFM) guidelines, rules, and common issues to ensure your circuit boards can be reliably produced.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|April 16, 2026
Best PCB Routing Techniques for Clean Circuit Board Layouts

Best PCB Routing Techniques for Clean Circuit Board Layouts

Learn the best PCB routing techniques for clean circuit board layouts, including trace routing tips, differential pair routing, and layout best practices.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|April 16, 2026
High-Speed PCB Design: Layout Rules, Signal Integrity, and Routing Best Practices

High-Speed PCB Design: Layout Rules, Signal Integrity, and Routing Best Practices

Whether you're migrating from popular EDA applications or starting fresh, mastering high speed PCB design has never been more intuitive. Flux enables teams to design, simulate, and route with real-time AI assistance, so you can spin your next high-speed board with total confidence.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|March 26, 2026
Design Rule Checking (DRC) in PCB Design: Real-Time vs Batch, Rules, and Common Failures

Design Rule Checking (DRC) in PCB Design: Real-Time vs Batch, Rules, and Common Failures

DRC is an automated process that checks your PCB layout against manufacturing and electrical constraints, catching errors like trace spacing and drill sizes before fabrication. Modern tools run this in real-time during design, while older ones batch-check at the end, often producing overwhelming error lists.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|March 26, 2026
What Is a PCB? A Beginner's Guide to Printed Circuit Board Design

What Is a PCB? A Beginner's Guide to Printed Circuit Board Design

Whether you are exploring “What is a PCB?” for the first time or moving into advanced hardware engineering, modern tools make the process easier than ever. With Flux's AI-assisted platform, you can skip the steep learning curve of popular ECAD applications and design collaboratively directly in your browser. Once your board is routed and ready for fabrication, Flux's built-in supply chain features connect you directly with worldwide distributors to source parts instantly. Sign up for free today and start building!

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|March 21, 2026
Simulate Circuits with a Prompt

Simulate Circuits with a Prompt

Flux brings circuit simulation to wherever you are in the design process. Start from a prompt when you have no schematic, or let Flux analyze your existing design automatically.

Profile avatar of Lance Cassidy
Lance Cassidy
|March 20, 2026