3.12. Automatically Generated Types
In this chapter, you'll learn about the types Medusa automatically generates under the .medusa
directory and how you should use them.
What are Automatically Generated Types?#
Medusa automatically generates TypeScript types for:
- Data models collected in the Query's graph. These types provide you with auto-completion and type checking when using Query.
- Generated data model types are located in the
.medusa/types/query-entry-points.d.ts
file.
- Generated data model types are located in the
- Modules registered in the Medusa container. These types provide you with auto-completion and type checking when resolving modules from the container.
- Generated module registration names are located in the
.medusa/types/modules-bindings.d.ts
file.
- Generated module registration names are located in the
How to Trigger Type Generation?#
The Medusa application generates these types automatically when you run the application with the dev
command:
So, if you add a new data model or module and you don't find it in auto-completion or type checking, you can run the dev
command to regenerate the types.
How to Use the Generated Types?#
The generated types are only meant for auto-completion and type checking.
For example, consider you have a Brand Module with a Brand
data model. Due to the auto-generated types, you can do the following:
Don't Import the Generated Types#
The generated types are only meant to help you in your development process by providing auto-completion and type checking. You should not import them directly in your code.
Since you don't commit the .medusa
directory to your version control system or your production environment, importing these types may lead to build errors.
Instead, if you need to use a data model's type in your customizations, you can use the InferTypeOf utility function, which infers the type of a data model based on its properties.
For example, if you want to use the Brand
data model's type in your customizations, you can do the following:
You can then use the BrandType
type in your customizations, such as in workflow inputs or service method outputs.