TypeScript Generation for Headless WordPress
Pro: Field Forge generates TypeScript type definitions (.d.ts files) for every field group on your WordPress site. Most field types are mapped to a TypeScript type, and a type with no mapping becomes unknown. A Repeater field becomes an array of an object type made of its sub-fields. Flexible content becomes discriminated unions. The result: TypeScript types for the custom fields that your JavaScript frontend reads.
The headless WordPress type-safety problem
Headless WordPress means WordPress serves as the CMS and a separate JavaScript frontend (Next.js, Astro, Nuxt, etc.) renders the pages. The frontend fetches content via REST API or WPGraphQL and displays it. For simple content — title, excerpt, featured image — Some SDKs ship TypeScript types for them.
Custom fields are the problem. Custom fields are plugin-defined, not core WordPress. TypeScript doesn’t know that your “Hero Section” field group has a title: string, subtitle: string, background_image: WPImage, and cta_button: { text: string; url: string; }. Frontend code that accesses data.hero_title has no type safety — typos, missing fields, and field-rename refactors all become runtime errors.
The solutions that existed before Field Forge:
- Write types by handtedious and drifts out of sync when you edit field groups in WordPress
- Use a generic
anydefeats the purpose of TypeScript - Use Zod or similar runtime validationadds boilerplate to every API call
- Use code generation from GraphQL schemaworks if you use WPGraphQL + GraphQL Code Generator, but complex to set up
Field Forge generates TypeScript definitions directly from your field groups.
How it works
Step 1: Build a field group in Field Forge
You design a field group in the visual builder — let’s say “Hero Section” with fields for title, subtitle, background image, and CTA button.
Step 2: Press “Generate TypeScript”
In Field Forge → Tools, the Import / Export part has a Headless Types card (Pro) with a Generate TypeScript button. It shows the type definitions for all your field groups, with Copy and Download buttons for the result. Download saves the text on the screen as fieldforge.d.ts.
Step 3: Use the types in your frontend
Commit the downloaded file to your frontend repo (or copy and paste it into an existing types file). TypeScript now knows the shape of the data of every field group.
import type { HeroSection } from './types/fieldforge';
function Hero({ data }: { data: HeroSection }) {
return (
<section>
<h1>{data.title}</h1>
<p>{data.subtitle}</p>
<img src={data.background_image.url} alt={data.background_image.alt} />
<a href={data.cta_button.url}>{data.cta_button.text}</a>
</section>
);
}
With the generated types, the TypeScript compiler reports a misspelled field name or a wrong type at build time, and an editor can offer field names as you type.
Field type mapping
The generator turns each field into a TypeScript type as the table shows. A field is optional (name?: type) unless it is marked Required.
| Field Forge field | TypeScript type |
|---|---|
| Text | string |
| Textarea | string |
| Number | number |
| Range | number |
string |
|
| URL | string |
| Password | string |
| Image | WPImage (string for return format url, number for id) |
| File | WPFile (string for return format url, number for id) |
| WYSIWYG Editor | string |
| oEmbed | string |
| Gallery | WPImage[] (string[] for url, number[] for id) |
| Select | a union of the choice values, for example 'a' | 'b'; ('a' | 'b')[] when the field allows multiple values; string (string[] for multiple values) when the field has no choices |
| Checkbox | ('a' | 'b')[] (string[] when the field has no choices) |
| Radio Button | a union of the choice values, or string when the field has no choices |
| True / False | boolean |
| Button Group | a union of the choice values, or string when the field has no choices |
| Relationship | WPPost[] (number[] for id) |
| Post Object | WPPost (WPPost[] when the field allows multiple posts; number or number[] for id) |
| Page Link | string |
| Taxonomy | WPTerm | WPTerm[] (number | number[] for id) |
| User | WPUser (WPUser[] when the field allows multiple users; number or number[] for id) |
| Link | unknown |
| Date Picker | string |
| Time Picker | string |
| Color Picker | string |
| Google Map | unknown |
| Tab, Accordion, Message | left out of the types |
| Calculation | unknown |
| Repeater | an array of an object type made of its sub-fields (Array<{ ... }>) |
| Group | an object type made of its sub-fields |
| Flexible Content | an array of the union of its layouts, each an object type with acf_fc_layout set to the name of the layout |
| Clone | unknown |
The return format of Image, File, Gallery, Post Object, Relationship, Taxonomy and User decides the type, as the table shows, and so does the multiple setting of Select, Post Object and User. The field group editor has no input for the return format or for the multiple setting; a field imported from ACF can carry them.
Built-in types
The generated file contains these interfaces, which the types above refer to:
export interface WPImage {
ID: number;
url: string;
width: number;
height: number;
alt: string;
title: string;
sizes: Record<string, string>;
}
export interface WPFile {
ID: number;
url: string;
filename: string;
title: string;
filesize: number;
mime_type: string;
}
export interface WPPost {
ID: number;
post_title: string;
post_name: string;
post_type: string;
post_status: string;
permalink: string;
}
export interface WPUser {
ID: number;
display_name: string;
user_email: string;
}
export interface WPTerm {
term_id: number;
name: string;
slug: string;
taxonomy: string;
}
The generator writes these interfaces as they are; it does not read the values of a site, and some members are not filled by the plugin. An Image with the array return format has ID, url, width, height, alt and title, and a File with the array return format has ID, url, filename and title: no Field Forge code sets sizes, filesize or mime_type. A Post Object, Taxonomy or User field with the object return format, or with no return format set, returns the WordPress objects themselves (WP_Post, WP_Term or WP_User), and Field Forge adds no permalink to a post. A User field with the array return format returns an array with ID, display_name and user_email, the shape of WPUser. A Relationship field saved from the post editor is not an array of WPPost: with one post chosen it returns that post (a WP_Post object, or the ID with the id return format), and with several posts chosen it returns their IDs as text such as 16,17. A Relationship value stored as an array of IDs, for example one imported from ACF or written with update_field(), comes back as an array (of posts, or of IDs with the id return format).
Example: Hero field group
A field group called “Hero Section” with a required title, an optional subtitle, a required background image, and a required CTA button (a Group with the sub-fields text, url and open_in_new_tab) generates the interface HeroSection. The interface is named after the group title: the generator keeps the letters A to Z, the digits and the spaces of the title and drops every other character, makes the letters lower case, starts each word with a capital letter and removes the spaces. To choose the name yourself, give the group a title in Latin letters that starts with a letter.
export interface HeroSection {
/** Title */
title: string;
/** Subtitle */
subtitle?: string;
/** Background image */
background_image: WPImage;
/** CTA button */
cta_button: {
text: string;
url: string;
open_in_new_tab?: boolean;
};
}
Usage in a Next.js component:
import type { HeroSection } from '@/types/fieldforge';
interface Props {
hero: HeroSection;
}
export function Hero({ hero }: Props) {
return (
<section className="hero">
<img
src={hero.background_image.url}
alt={hero.background_image.alt}
width={hero.background_image.width}
height={hero.background_image.height}
/>
<h1>{hero.title}</h1>
<p>{hero.subtitle}</p>
<a
href={hero.cta_button.url}
target={hero.cta_button.open_in_new_tab ? '_blank' : undefined}
>
{hero.cta_button.text}
</a>
</section>
);
}
Every property is typed. TypeScript catches hero.titel (typo), hero.background_image.URL (wrong case), and hero.cta_button.open_new_tab (wrong field name) at compile time.
Repeater types
A Repeater becomes Array<{ ... }> of an object type made of its sub-fields, and a sub-field that is marked Required has no ?. A Repeater inside a Repeater is typed the same way. The generator indents nested objects loosely; the examples on this page are indented for reading.
export interface TeamPage {
/** Team members */
team_members: Array<{
name: string;
photo: WPImage;
bio?: string;
skills?: Array<{
skill_name: string;
proficiency?: number;
}>;
}>;
}
TypeScript understands the nesting. Iterating over team_members and then skills is fully typed.
Flexible content as discriminated unions
Flexible content fields generate discriminated unions — TypeScript’s native feature for handling multi-type arrays. Each layout becomes an object type whose acf_fc_layout is the name of the layout, and the field is an array of the union of the layouts:
export interface LandingPage {
/** Page sections */
page_sections: Array<{
acf_fc_layout: 'hero';
title: string;
subtitle?: string;
background_image?: WPImage;
} | {
acf_fc_layout: 'features';
section_title?: string;
features?: Array<{
icon?: string;
title: string;
description?: string;
}>;
} | {
acf_fc_layout: 'cta';
headline: string;
button?: {
text: string;
url: string;
};
}>;
}
In your frontend component, use a type guard to render the right layout:
function PageSections({ sections }: { sections: LandingPage['page_sections'] }) {
return (
<>
{sections.map((section, i) => {
if (section.acf_fc_layout === 'hero') {
return <Hero key={i} data={section} />;
}
if (section.acf_fc_layout === 'features') {
return <Features key={i} data={section} />;
}
if (section.acf_fc_layout === 'cta') {
return <CTA key={i} data={section} />;
}
})}
</>
);
}
Inside each if block, TypeScript narrows section to the correct type. No casting needed.
Regeneration workflow
The definitions are made when you press Generate TypeScript; they do not change by themselves when you edit a field group. To keep your frontend in sync:
- Edit the field group in WordPress
- Open Field Forge → Tools, press Generate TypeScript, then Download
- Replace the
fieldforge.d.tsfile in your frontend repo - Commit the change
Field Forge has no REST route for the TypeScript definitions: you get them from the buttons in the admin.
Using the types
The file holds plain TypeScript declarations (.d.ts), so a TypeScript project imports it like any other declaration file. Field Forge has no framework-specific code.
Ready for type-safe headless WordPress?
TypeScript generation is included in every paid plan.