#Can I access the type of of an item in a document?

1 messages · Page 1 of 1 (latest)

proven hazel
#

I want to access the type of a property of something in ym Doc.

e.g., Doc<"invoices">.lineItems

Where I'm trying to get the type of lineItems.

stable tinselBOT
#

Hi @proven hazel. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can access the type of a property of a document in Convex using TypeScript's indexed access types. Once you have the generated Doc<"invoices"> type, you can get the type of the lineItems property like this:

import { Doc } from "../convex/_generated/dataModel";

type LineItems = Doc<"invoices">["lineItems"];

This will give you the exact type of the lineItems property as defined in your schema. This approach works for any property of a document type generated by Convex. If you need the type of an item within an array property (e.g., if lineItems is an array), you can use:

type LineItem = Doc<"invoices">["lineItems"][number];

This extracts the type of a single item in the lineItems array.

This method is standard TypeScript and is compatible with the types generated by Convex after you define your schema and run npx convex dev to generate the types in dataModel.d.ts TypeScript types from schema.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: