Skip to content

feat: add typescript.postProcess hook for type generation#16103

Merged
JarrodMFlesch merged 2 commits into
mainfrom
worktree-elegant-sparking-stallman
Apr 1, 2026
Merged

feat: add typescript.postProcess hook for type generation#16103
JarrodMFlesch merged 2 commits into
mainfrom
worktree-elegant-sparking-stallman

Conversation

@JarrodMFlesch

@JarrodMFlesch JarrodMFlesch commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Summary

This pr would allow us to import a generic from the plugin and then use it. The limitation with json-schema-to-typescript is the inability to generate a type in the file that takes generics.

There are some generated generics plopped in, internally by our addSelectGenericsToGeneratedTypes function.

This PR exposes a way for external plugins to manipulate the generated config post-processing of json-schema-to-typescript which is not possible today.

Implementation

In packages/payload/src/bin/generateTypes.ts, after addSelectGenericsToGeneratedTypes:

if (config.typescript?.postProcess?.length) {
  for (const fn of config.typescript.postProcess) {
    compiled = fn(compiled);
  }
}

Add to TypeScript config type:

typescript?: {
  postProcess?: Array<(compiled: string) => string>;
}

Use cases

  • Inject imports from plugin packages
  • Add utility types

Example

// Plugin adds an import and uses it via tsType on fields

typescript: {
  postProcess: [
    ...(config.typescript?.postProcess || []),
    (compiled) => {
      const importStmt = `import type { MyPluginType } from '@payloadcms/my-plugin';`;
      return compiled.replace(/(\/\*[\s\S]*?\*\/\n)/, `$1\n${importStmt}\n`);
    },
  ],
}

Notes

  • Follows existing pattern (Payload already post-processes for Select generics)

Enables plugins to post-process generated TypeScript types before writing
to file. Useful for injecting generic types that JSON Schema cannot express.
@github-actions

github-actions Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@JarrodMFlesch JarrodMFlesch enabled auto-merge (squash) April 1, 2026 13:39
@JarrodMFlesch JarrodMFlesch merged commit 67b5dc3 into main Apr 1, 2026
305 of 310 checks passed
@JarrodMFlesch JarrodMFlesch deleted the worktree-elegant-sparking-stallman branch April 1, 2026 13:39
@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.82.0

milamer pushed a commit to milamer/payload that referenced this pull request Apr 20, 2026
…#16103)

## Summary

This pr would allow us to import a generic from the plugin and then use
it. The limitation with json-schema-to-typescript is the inability to
generate a type in the file that takes generics.

There are some generated generics plopped in, internally by our
addSelectGenericsToGeneratedTypes function.

This PR exposes a way for external plugins to manipulate the generated
config post-processing of json-schema-to-typescript which is not
possible today.

## Implementation

In `packages/payload/src/bin/generateTypes.ts`, after
`addSelectGenericsToGeneratedTypes`:

```ts
if (config.typescript?.postProcess?.length) {
  for (const fn of config.typescript.postProcess) {
    compiled = fn(compiled);
  }
}
```

Add to TypeScript config type:
```ts
typescript?: {
  postProcess?: Array<(compiled: string) => string>;
}
```

**Use cases**

- Inject imports from plugin packages
- Add utility types

**Example**

```ts
// Plugin adds an import and uses it via tsType on fields

typescript: {
  postProcess: [
    ...(config.typescript?.postProcess || []),
    (compiled) => {
      const importStmt = `import type { MyPluginType } from '@payloadcms/my-plugin';`;
      return compiled.replace(/(\/\*[\s\S]*?\*\/\n)/, `$1\n${importStmt}\n`);
    },
  ],
}
```

**Notes**

- Follows existing pattern (Payload already post-processes for Select
generics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants