Payload
Blog Post
New Feature - Auto-generate TypeScript Interfaces
New Feature - Auto-generate TypeScript Interfaces

Payload just shipped a ton of new TypeScript features and improvements. Most notably—if you're using Payload with TypeScript, you can now generate types automatically for all of your collections and globals to use within your app's code.

We've been working hard at making Payload + TypeScript a match made in heaven, and with its newest version (0.13.6), building in TypeScript just got a lot better. Most notably, we've shipped a new command that allows you to generate TypeScript types automatically from all your Global and Collection configs:

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.

For an example of how powerful this is, let's look at the following example config:

const config: Config = {
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
admin: {
user: 'users',
},
collections: [
{
slug: 'users',
fields: [
{
name: 'name',
type: 'text',
required: true,
}
]
},
{
slug: 'posts',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
{
name: 'author',
type: 'relationship',
relationTo: 'users',
},
{
name: 'status',
type: 'select',
required: true,
options: ['published', 'draft'],
}
]
}
]
}

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

export interface User {
id: string;
name: string;
email?: string;
resetPasswordToken?: string;
resetPasswordExpiration?: string;
loginAttempts?: number;
lockUntil?: string;
}
export interface Post {
id: string;
title?: string;
author?: string | User;
status: 'published' | 'draft'
}

All of your fields are automatically converted into their TypeScript equivalent properties. Even relationship fields are typed properly and will reference your other collection interfaces accurately.

Customizing the output path of your generated types

Specifying where you want your types to be generated is easy. Just add a property to your Payload config:

{
// the remainder of your config
typescript: {
outputFile: path.resolve(__dirname, './generated-types.ts'),
},
}

The above example places your types next to your Payload config itself as the file generated-types.ts. By default, the file will be output to your current working directory as payload-types.ts.

Using generics in your Payload app functions

By providing the types to your hook, access control and validation functions your code editor can now give you typeahead suggestions. Writing code with autocompletion from TypeScript vastly improves the developer experience.

To demonstrate some of the new TypeScript functionality that Payload offers, let's build a way to automatically set the author of a Posts collection to the currently logged-in user if no author is manually set.

Here is an example of a Field hook for the author field of the above Posts collection:

import { FieldHook } from 'payload/types';
import { Post, User } from '../generated-types';
// The `FieldHook` type is a generic that accepts two types:
// 1. The type of the collection or global that the field is assigned to,
// which is used to type the `originalDoc` and `data`
// 2. The type of the value that the field itself stores
// which will be used to type the `value` arg
const populateAuthor: FieldHook<Post, Post['author']> = (args) => {
const {
value, // this is typed to the Author field of our Post collection automatically
originalDoc, // This is typed to an entire Post
data, // This is a Partial<Post>
operation,
req,
} = args;
// If there is no value set manually
// and only in the `create` operation
if (operation === 'create' && !value) {
return req.user.id;
}
// Otherwise just return whatever the incoming value is
return value;
}

There is a lot of power with Payload's new functionality, and we hope you're excited! We'd also love to know what you think. Stop by our GitHub Discussions board to join in on the conversations and help shape the future of Payload.

Documentation

Complete information on generating types can be viewed in our documentation.

More on the way

In addition to the TypeScript updates and improvements we've mentioned in this post, our Roadmap features a few more features and updates that will further solidify Payload as the most advanced and feature-packed headless CMS available.

Next up: Revisions, Autosave, Drafts, and loading UI improvements! Keep an eye out!