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:

1
payload generate:types

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.

1
// payload.config.ts
2
{
3
// ...
4
typescript: {
5
declare: false, // defaults to true if not set
6
},
7
}

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:

1
import { Config } from './payload-types'
2
3
declare module 'payload' {
4
export interface GeneratedTypes extends Config {}
5
}

Custom output file path

You can specify where you want your types to be generated by adding a property to your Payload config:

1
// payload.config.ts
2
{
3
// ...
4
typescript: {
5
// defaults to: path.resolve(__dirname, './payload-types.ts')
6
outputFile: path.resolve(__dirname, './generated-types.ts'),
7
},
8
}

The above example places your types next to your Payload config itself as the file generated-types.ts.

Example Usage

For example, let's look at the following simple Payload config:

1
const config: Config = {
2
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
3
admin: {
4
user: 'users',
5
}
6
collections: [
7
{
8
slug: 'users',
9
fields: [
10
{
11
name: 'name',
12
type: 'text',
13
required: true,
14
}
15
]
16
},
17
{
18
slug: 'posts',
19
admin: {
20
useAsTitle: 'title',
21
},
22
fields: [
23
{
24
name: 'title',
25
type: 'text',
26
},
27
{
28
name: 'author',
29
type: 'relationship',
30
relationTo: 'users',
31
},
32
]
33
}
34
]
35
}

By generating types, we'll end up with a file containing the following two TypeScript interfaces:

1
export interface User {
2
id: string
3
name: string
4
email?: string
5
resetPasswordToken?: string
6
resetPasswordExpiration?: string
7
loginAttempts?: number
8
lockUntil?: string
9
}
10
11
export interface Post {
12
id: string
13
title?: string
14
author?: string | User
15
}

Custom Field Interfaces

For array, block, group and named tab fields, you can generate top level reusable interfaces. The following group field config:

1
{
2
type: 'group',
3
name: 'meta',
4
interfaceName: 'SharedMeta', <-- here!!
5
fields: [
6
{
7
name: 'title',
8
type: 'text',
9
},
10
{
11
name: 'description',
12
type: 'text',
13
},
14
],
15
}

will generate:

1
// a top level reusable interface!!
2
export interface SharedMeta {
3
title?: string
4
description?: string
5
}
6
7
// example usage inside collection interface
8
export interface Collection1 {
9
// ...other fields
10
meta?: SharedMeta
11
}

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:

1
{
2
"scripts": {
3
"generate:types": "PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types",
4
},
5
}

Now you can run yarn generate:types to easily generate your types.

Next

Plugins