Skip to content

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

Open
JarrodMFlesch wants to merge 1 commit intomainfrom
worktree-elegant-sparking-stallman
Open

feat: add typescript.postProcess hook for type generation#16103
JarrodMFlesch wants to merge 1 commit intomainfrom
worktree-elegant-sparking-stallman

Conversation

@JarrodMFlesch
Copy link
Copy Markdown
Contributor

@JarrodMFlesch JarrodMFlesch commented Mar 30, 2026

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
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! 🙌

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.

1 participant