Incremental strictNullChecks adoption - Slice X (add contentScript/messenger/api.ts)
Follow-up of https://github.com/pixiebrix/pixiebrix-extension/issues/6498
contentScript/messenger/api.ts is being split off because of the 700 ts errors that need to be resolved from adding it
We'll keep this one on the back-burner for now. Once other low-hanging fruit have been fixed, we'll try again.
As of https://github.com/pixiebrix/pixiebrix-extension/pull/6590, there are still 605 errors that would need to be resolved.
As discussed with Graham on Slack, this file is high up in the suggested files to fix because it's a funnel file:
-
api.tshas 48 exports. In reality, each export is not as important on its own as this metric makes it seem.
We should essentially ignore this these files and treat them as entry points:
-
registration.tsimports from lots of parts of the extension (30 direct imports + their sub dependencies).
I suggest tackling the rest of the migration list for now, these files will naturally trigger fewer errors.
We could alternatively add each of those 30 imports to the strictNullChecks config, but again individually they're not as important as they seem.
Need to remove temporary typing in src/sidebar/messenger/registration.ts. See https://github.com/pixiebrix/pixiebrix-extension/pull/6650/files#r1355092186
We can unblock many of these files by creating additional entry points for common methods like recordEvent, which is imported directly by 60 files:
- import { recordEvent } from "@/background/messenger/api";
+ import { recordEvent } from "@/background/messenger/api-strict";
or
+ import { recordEvent } from "@/background/strict/messenger/api";
This, together with a "strict" registration.ts file, can help migrate more and more common methods to being strict, instead of having to tackle all of the 700 errors in a single PR (or never tackling this at all)
We can unblock many of these files by creating additional entry points for common methods like
recordEvent, which is imported directly by 60 files:- import { recordEvent } from "@/background/messenger/api"; + import { recordEvent } from "@/background/messenger/api-strict"; or + import { recordEvent } from "@/background/strict/messenger/api";This, together with a "strict" registration.ts file, can help migrate more and more common methods to being strict, instead of having to tackle all of the 700 errors in a single PR (or never tackling this at all)
This sounds like a good approach.
It is also worth noting that adding the two files now generates 570 errors, so working on other files first is slowly decreasing the overall load here.
If you encounter the keyof MessengerMethods issue when working on unrelated files:
- reorganize the code to avoid causing the non-strict messenger to be imported, e.g. don't place your new code in a non-strict file that uses the Messenger.
- if you're directly importing a method from
messenger/api.ts, try moving it tomessenger/strict/api.tsand fixing the errors as suggested in the section below.
Here's how to gradually migrate messenger files:
- Move an import from
registration.tstostrict/registration.ts(the whole imported file, not just a method) - Fix the type errors that arise, if possible
- Move its methods from
api.tstostrict/api.tsas well. - Update the imports and mocks across the extension