Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): fix TS2322 in the developer category beta gate

Kris Oeib4484f5c459d
diff --git a/apps/api/src/controllers/v2/search.ts b/apps/api/src/controllers/v2/search.ts
index f712badc5..c5ba383e2 100644
--- a/apps/api/src/controllers/v2/search.ts
+++ b/apps/api/src/controllers/v2/search.ts
@@ -40,6 +40,7 @@ import {
formatTypesOf,
} from "../../lib/key-restriction";
import {
+ isCategoryStringArray,
stripDeveloperCategory,
wantsDeveloperCategory,
} from "../../search/developer";
@@ -94,9 +95,20 @@ export async function searchController(
logger.info(
"developer category requested without developerBeta flag; dropping",
);
- req.body.categories = stripDeveloperCategory(
- req.body.categories as CategoryOption[],
- );
+ // Narrow first: req.body.categories is a union of two homogeneous
+ // arrays (all-string or all-object), and calling the generic helper
+ // directly on that union collapses T to a mixed type that no longer
+ // matches either branch on assignment back. Branching on the guard
+ // lets each call infer T concretely, so both assignments typecheck
+ // with no cast.
+ const categories = req.body.categories;
+ if (categories !== undefined) {
+ if (isCategoryStringArray(categories)) {
+ req.body.categories = stripDeveloperCategory(categories);
+ } else {
+ req.body.categories = stripDeveloperCategory(categories);
+ }
+ }
}
}
diff --git a/apps/api/src/search/developer.ts b/apps/api/src/search/developer.ts
index 491a5cfef..928936ce9 100644
--- a/apps/api/src/search/developer.ts
+++ b/apps/api/src/search/developer.ts
@@ -18,9 +18,9 @@ export function wantsDeveloperCategory(categories?: CategoryOption[]): boolean {
* developerBeta gate: an unentitled team's search proceeds without it rather
* than erroring.
*/
-export function stripDeveloperCategory(
- categories?: CategoryOption[],
-): CategoryOption[] | undefined {
+export function stripDeveloperCategory<T extends CategoryOption>(
+ categories?: T[],
+): T[] | undefined {
if (categories === undefined) {
return undefined;
}
@@ -31,6 +31,21 @@ export function stripDeveloperCategory(
);
}
+/**
+ * The zod-inferred category type on SearchRequest is a union of two
+ * *homogeneous* arrays (all-string or all-object), not a single array of a
+ * union type. TypeScript's generic inference collapses that union to a mixed
+ * `(string | CategoryInput)[]` when `stripDeveloperCategory` is called
+ * directly on it, which no longer matches either branch on the way back out.
+ * Narrowing with this predicate first lets each call site infer `T` as the
+ * concrete branch, so the round trip typechecks with no cast.
+ */
+export function isCategoryStringArray(
+ categories: CategoryOption[],
+): categories is string[] {
+ return categories.every(category => typeof category === "string");
+}
+
export async function searchDeveloperCategory(
options: { query: string; limit: number; teamId: string; timeout: number },
logger: Logger,