Skip to content

Commit 3d79472

Browse files
ianmacartneyConvex, Inc.
authored andcommitted
add Id to the generated api.d.ts file when doing static type generation (#42050)
We don't currently define the `Id` type that we reference when doing static api.d.ts generation - leading to any ID in those types being treated as `any` (not validated on arguments or useful in return values). GitOrigin-RevId: f5e18f583d3328e2e14229ae71c25b6561705a0d
1 parent 568d932 commit 3d79472

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/cli/codegen_templates/component_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ async function* codegenStaticApiObjects(
135135
analysis: EvaluatedComponentDefinition,
136136
) {
137137
yield `import type { FunctionReference } from "convex/server";`;
138+
yield `import type { GenericId as Id } from "convex/values";`;
138139

139140
const apiTree = await buildApiTree(ctx, analysis.functions, {
140141
kind: "public",

src/cli/codegen_templates/dataModel.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ async function* codegenTable(ctx: Context, table: TableDefinition) {
261261
for (const index of table.vectorIndexes ?? []) {
262262
yield ` "${index.indexDescriptor}": {`;
263263
yield ` vectorField: "${index.vectorField}",`;
264-
yield ` dimensions: ${index.dimensions},`;
264+
yield ` dimensions: number,`;
265265
yield ` filterFields: ${stringLiteralUnionType(index.filterFields)},`;
266266
yield ` },`;
267267
}
@@ -340,14 +340,19 @@ async function addSystemFieldsToObject(
340340
function* extractFieldPaths(validator: ConvexValidator): Generator<string[]> {
341341
if (validator.type === "object") {
342342
for (const [fieldName, fieldValidator] of Object.entries(validator.value)) {
343+
yield [fieldName];
343344
for (const subFieldPath of extractFieldPaths(fieldValidator.fieldType)) {
344-
yield [fieldName, ...subFieldPath];
345+
if (subFieldPath.length > 0) {
346+
yield [fieldName, ...subFieldPath];
347+
}
345348
}
346349
}
347350
} else if (validator.type === "union") {
348351
for (const subValidator of validator.value) {
349352
yield* extractFieldPaths(subValidator);
350353
}
354+
} else if (validator.type === "record") {
355+
yield ["${string}"];
351356
} else {
352357
yield [];
353358
}
@@ -356,9 +361,9 @@ function* extractFieldPaths(validator: ConvexValidator): Generator<string[]> {
356361
function stringLiteralUnionType(fields: string[]) {
357362
if (fields.length === 0) {
358363
return "never";
359-
} else if (fields.length === 1) {
360-
return `"${fields[0]}"`;
361364
} else {
362-
return fields.map((field) => `"${field}"`).join(" | ");
365+
return fields
366+
.map((field) => (field.includes("${") ? `\`${field}\`` : `"${field}"`))
367+
.join(" | ");
363368
}
364369
}

0 commit comments

Comments
 (0)