3.5.1. Infer Type of Data Model

In this chapter, you'll learn how to infer the type of a data model.

How to Infer Type of Data Model?#

Consider you have a Post data model. You can't reference this data model in a type, such as a workflow input or service method output types, since it's a variable.

Instead, Medusa provides InferTypeOf that transforms your data model to a type.

For example:

Code
1import { InferTypeOf } from "@medusajs/framework/types"2import { Post } from "../modules/blog/models/post" // relative path to the model3
4export type Post = InferTypeOf<typeof Post>

InferTypeOf accepts as a type argument the type of the data model.

Since the Post data model is a variable, use the typeof operator to pass the data model as a type argument to InferTypeOf.

You can now use the Post type to reference a data model in other types, such as in workflow inputs or service method outputs:

Example Service
1// other imports...2import { InferTypeOf } from "@medusajs/framework/types"3import { Post } from "../models/post"4
5type Post = InferTypeOf<typeof Post>6
7class BlogModuleService extends MedusaService({ Post }) {8  async doSomething(): Promise<Post> {9    // ...10  }11}
Was this chapter helpful?
Ask Anything
Ask any questions about Medusa. Get help with your development.
You can also use the Medusa MCP server in Cursor, VSCode, etc...
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break