DEV Community

i-am-killvish
i-am-killvish

Posted on

Extending my TypeScript auto-fix CLI to handle .filter(Boolean) narrowing issues

A few days ago I shared a small CLI experiment called fixmyfile that automatically fixes repetitive TypeScript errors using compiler diagnostics and AST transformations.

While testing it more, I kept running into another frustrating TypeScript pattern:

const users = data.filter(Boolean);

users.map((u) => u.name);
Enter fullscreen mode Exit fullscreen mode

Even after filtering values, TypeScript can still complain that:

'u' is possibly undefined
Enter fullscreen mode Exit fullscreen mode

Logically the values are already filtered, but TypeScript narrowing does not always behave the way developers expect.

So I started experimenting with AST-based transformations for these cases too.

Now the CLI automatically converts:

const users = data.filter(Boolean);
Enter fullscreen mode Exit fullscreen mode

into:

const users = data.filter(
  (x): x is NonNullable<typeof x> => Boolean(x)
);
Enter fullscreen mode Exit fullscreen mode

which preserves narrowing correctly and removes the repeated "possibly undefined" friction afterward.

What’s been interesting while building this project is realizing how many TypeScript frustrations are not necessarily difficult problems, but repetitive workflow interruptions developers hit every day.

Still very experimental, but AST transformations are turning out to be much more powerful than simple text replacements.

Current fixes supported:

  • Missing function arguments (TS2554)
  • Basic type mismatch fixes (TS2322)
  • Undefined references (TS2304)
  • .filter(Boolean) narrowing transformations

GitHub:
https://github.com/i-am-killvish/fixmyfile

Would love feedback on:

  • other repetitive TS friction points
  • weird narrowing cases
  • compiler issues developers repeatedly fight with

Top comments (0)