From 9cc1a96c1a9f9d805f5bb7a441d335b489efb013 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Mon, 24 Nov 2025 04:52:46 -0800 Subject: [PATCH] refactor: enhance type definitions in dataModel.d.ts for improved type safety and clarity by integrating schema-based types --- convex/_generated/dataModel.d.ts | 34 +++++++++++++++++--------------- convex/schema.ts | 14 +++++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 convex/schema.ts diff --git a/convex/_generated/dataModel.d.ts b/convex/_generated/dataModel.d.ts index fb12533..8541f31 100644 --- a/convex/_generated/dataModel.d.ts +++ b/convex/_generated/dataModel.d.ts @@ -8,29 +8,29 @@ * @module */ -import { AnyDataModel } from "convex/server"; +import type { + DataModelFromSchemaDefinition, + DocumentByName, + TableNamesInDataModel, + SystemTableNames, +} from "convex/server"; import type { GenericId } from "convex/values"; - -/** - * No `schema.ts` file found! - * - * This generated code has permissive types like `Doc = any` because - * Convex doesn't know your schema. If you'd like more type safety, see - * https://docs.convex.dev/using/schemas for instructions on how to add a - * schema file. - * - * After you change a schema, rerun codegen with `npx convex dev`. - */ +import schema from "../schema.js"; /** * The names of all of your Convex tables. */ -export type TableNames = string; +export type TableNames = TableNamesInDataModel; /** * The type of a document stored in Convex. + * + * @typeParam TableName - A string literal type of the table name (like "users"). */ -export type Doc = any; +export type Doc = DocumentByName< + DataModel, + TableName +>; /** * An identifier for a document in Convex. @@ -42,8 +42,10 @@ export type Doc = any; * * IDs are just strings at runtime, but this type can be used to distinguish them from other * strings when type checking. + * + * @typeParam TableName - A string literal type of the table name (like "users"). */ -export type Id = +export type Id = GenericId; /** @@ -55,4 +57,4 @@ export type Id = * This type is used to parameterize methods like `queryGeneric` and * `mutationGeneric` to make them type-safe. */ -export type DataModel = AnyDataModel; +export type DataModel = DataModelFromSchemaDefinition; diff --git a/convex/schema.ts b/convex/schema.ts new file mode 100644 index 0000000..8ac5e87 --- /dev/null +++ b/convex/schema.ts @@ -0,0 +1,14 @@ +import { authTables } from '@convex-dev/auth/server' +import { defineSchema } from 'convex/server' + +const schema = defineSchema( + { + ...authTables, + // your other tables... + }, + { + strictTableNameTypes: false, + } +) + +export default schema