magicseth
magicseth2y ago

Adding `_id` to inferred types

I'm happilly using the Infer type which I love. but it doesn't include an id field (as it might not have one) is there a recommended way to add an id property to an infer'ed type
2 Replies
alexcole
alexcole2y ago
Yay, glad you like it! Yup, it doesn't add _id because the type might not be an entire document (it could be just a piece of one). I think the best way to add the _id field is with:
import { Infer} from "convex/schema";
import { Id } from "./_generated/dataModel";

type MyType = Infer<typeof objectSchema> & { _id: Id<"myTableName"> };
import { Infer} from "convex/schema";
import { Id } from "./_generated/dataModel";

type MyType = Infer<typeof objectSchema> & { _id: Id<"myTableName"> };
Also obviously if the type is an entire document, it's easier to just use the Document<TableName> type.
magicseth
magicsethOP2y ago
(not obviously!) thank you

Did you find this page helpful?