Generating TypeScript Interfaces
While building your own custom functionality into Payload, like Plugins, Hooks, Access Control functions, Custom Views, GraphQL queries / mutations, or anything else, you may benefit from generating your own TypeScript types dynamically from your Payload Config itself.
Types generation script
Run the following command in a Payload project to generate types based on your Payload Config:
You can run this command whenever you need to regenerate your types, and then you can use these types in your Payload code directly.
Disable declare statement
By default, generate:types
will add a declare
statement to your types file, which automatically enables type inference within Payload.
If you are using your payload-types.ts
file in other repos, though, it might be better to disable this declare
statement, so that you don't get any TS errors in projects that use your Payload types, but do not have Payload installed.
If you do disable the declare
pattern, you'll need to manually add a declare
statement to your code in order for Payload types to be recognized. Here's an example showing how to declare your types in your payload.config.ts
file:
Custom output file path
You can specify where you want your types to be generated by adding a property to your Payload Config:
The above example places your types next to your Payload Config itself as the file generated-types.ts
.
Custom generated types
Payload generates your types based on a JSON schema. You can extend that JSON schema, and thus the generated types, by passing a function to typescript.schema
:
This function takes the existing JSON schema as an argument and returns the modified JSON schema. It can be useful for plugins that wish to generate their own types.
Example Usage
For example, let's look at the following simple Payload Config:
By generating types, we'll end up with a file containing the following two TypeScript interfaces:
Custom Field Interfaces
For array
, block
, group
and named tab
fields, you can generate top level reusable interfaces. The following group field config:
will generate:
Using your types
Now that your types have been generated, payloads local API will now be typed. It is common for users to want to use this in their frontend code, we recommend generating them with Payload and then copying the file over to your frontend codebase. This is the simplest way to get your types into your frontend codebase.
Adding an npm script
Payload will automatically try and locate your config, but might not always be able to find it. For example, if you are working in a /src
directory or similar, you need to tell Payload where to find your config manually by using an environment variable. If this applies to you, you can create an npm script to make generating your types easier.
To add an npm script to generate your types and show Payload where to find your config, open your package.json
and update the scripts
property to the following:
Now you can run pnpm generate:types
to easily generate your types.