|
| 1 | +--- |
| 2 | +title: TypeScript Plugin (Experimental) |
| 3 | +label: TypeScript Plugin |
| 4 | +order: 30 |
| 5 | +desc: IDE support for PayloadComponent import paths – inline validation, autocomplete, and go-to-definition. |
| 6 | +keywords: headless cms, typescript, documentation, ide, plugin, autocomplete, validation, import paths |
| 7 | +--- |
| 8 | + |
| 9 | +<Banner type="warning"> |
| 10 | + **Experimental** — This plugin is experimental and may change in future |
| 11 | + releases. Please [report any |
| 12 | + issues](https://github.com/payloadcms/payload/issues) you encounter. |
| 13 | +</Banner> |
| 14 | + |
| 15 | +Payload ships an optional TypeScript Language Service Plugin (`@payloadcms/typescript-plugin`) that enhances your IDE experience when working with [Custom Components](../custom-components/overview). It understands Payload's `PayloadComponent` import path conventions and provides: |
| 16 | + |
| 17 | +- **Path validation** — red squigglies when a component path doesn't resolve to a real file |
| 18 | +- **Export validation** — errors when the named export doesn't exist in the target module, with "Did you mean?" suggestions |
| 19 | +- **Autocomplete** — file and directory suggestions while typing the path, and export name suggestions after `#` |
| 20 | +- **Go-to-definition** — Ctrl/Cmd+click on a component path string to jump to the component's source |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +Install the plugin as a dev dependency: |
| 25 | + |
| 26 | +```bash |
| 27 | +pnpm add -D @payloadcms/typescript-plugin |
| 28 | +``` |
| 29 | + |
| 30 | +Then add it to the `plugins` array in your `tsconfig.json`: |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "compilerOptions": { |
| 35 | + "plugins": [{ "name": "next" }, { "name": "@payloadcms/typescript-plugin" }] |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### VS Code / Cursor Setup |
| 41 | + |
| 42 | +TypeScript language service plugins only load when the editor uses the **workspace version** of TypeScript (the one installed in your project's `node_modules`). By default, VS Code uses its own bundled version which won't load the plugin. |
| 43 | + |
| 44 | +To switch: open the command palette (`Cmd+Shift+P`) and run **"TypeScript: Select TypeScript Version"**, then choose **"Use Workspace Version"**. |
| 45 | + |
| 46 | +For teams, add the following to `.vscode/settings.json` so everyone is prompted to switch: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "typescript.tsdk": "node_modules/typescript/lib", |
| 51 | + "typescript.enablePromptUseWorkspaceTsdk": true |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +After selecting the workspace version, restart the TypeScript server (`Cmd+Shift+P` → "TypeScript: Restart TS Server"). |
| 56 | + |
| 57 | +<Banner type="info"> |
| 58 | + TypeScript Language Service Plugins only run inside your IDE. They do not |
| 59 | + affect `tsc` compilation or your build output. |
| 60 | +</Banner> |
| 61 | + |
| 62 | +## Supported Path Conventions |
| 63 | + |
| 64 | +The plugin supports all of Payload's component path formats: |
| 65 | + |
| 66 | +| Format | Example | Resolution | |
| 67 | +| ----------------------- | ---------------------------------- | ------------------------------ | |
| 68 | +| Absolute (from baseDir) | `'/components/MyField#MyField'` | Resolved relative to `baseDir` | |
| 69 | +| Relative | `'./components/MyField#MyField'` | Resolved relative to `baseDir` | |
| 70 | +| tsconfig alias | `'@/components/MyField#MyField'` | Resolved via tsconfig `paths` | |
| 71 | +| Package import | `'@payloadcms/ui/rsc#MyComponent'` | Resolved via node_modules | |
| 72 | +| Default export | `'/components/MyField'` | Uses `default` export | |
| 73 | + |
| 74 | +Both the **string form** and the **object form** are supported: |
| 75 | + |
| 76 | +```ts |
| 77 | +// String form |
| 78 | +{ |
| 79 | + admin: { |
| 80 | + components: { |
| 81 | + Field: '/components/MyField#MyField', |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Object form |
| 87 | +{ |
| 88 | + admin: { |
| 89 | + components: { |
| 90 | + Field: { |
| 91 | + path: '/components/MyField', |
| 92 | + exportName: 'MyField', |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Configuration |
| 100 | + |
| 101 | +### Base Directory |
| 102 | + |
| 103 | +The plugin automatically detects `baseDir` by walking up from the current file to find the nearest `payload.config.ts`. Absolute paths (starting with `/`) and relative paths (starting with `./`) are resolved relative to this directory. |
| 104 | + |
| 105 | +If your project uses a non-standard layout, you can override `baseDir` in the plugin config: |
| 106 | + |
| 107 | +```json |
| 108 | +{ |
| 109 | + "compilerOptions": { |
| 110 | + "plugins": [ |
| 111 | + { |
| 112 | + "name": "@payloadcms/typescript-plugin", |
| 113 | + "baseDir": "./src" |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +The `baseDir` path is relative to the `tsconfig.json` location. |
| 121 | + |
| 122 | +## How It Works |
| 123 | + |
| 124 | +The plugin detects `PayloadComponent` positions by checking the contextual type of string literals in your config. Any string typed as `PayloadComponent`, `CustomComponent`, or any type that resolves to the same `false | RawPayloadComponent | string` union shape is automatically validated. |
| 125 | + |
| 126 | +This means the plugin works everywhere Payload expects a component path — collection field components, global components, admin panel components, dashboard widgets, and custom views — without needing to hardcode specific config positions. |
0 commit comments