You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: document exported TypeScript type helpers for plugins (#17336)
adds a Type Helpers section to the TypeScript overview outlining the
generic helpers exported from `payload`
(`CollectionSlug`,`DataFromCollectionSlug`, `TypedUser`, etc.) and how
they resolve via `GeneratedTypes`
If possible, include [JSDoc comments](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#types-1) to describe the options and their types. This allows a developer to see details about the options in their editor.
275
275
276
+
### Consuming generated types
277
+
278
+
A common question when building a plugin is how to reference the type of a collection that belongs to the _end user_. At authoring time you don't know which collections the user has, so it's tempting to hand-copy a subset of their interfaces — but that is fragile and quickly drifts out of sync.
279
+
280
+
Instead, use Payload's generic [type helpers](../typescript/overview#type-helpers). Because a consuming project augments the `GeneratedTypes` interface when it runs `payload generate:types`, helpers like `CollectionSlug` and `DataFromCollectionSlug` resolve to the user's _actual_ collections wherever your plugin's source is type-checked in their project:
281
+
282
+
```ts
283
+
importtype {
284
+
CollectionSlug,
285
+
DataFromCollectionSlug,
286
+
CollectionAfterChangeHook,
287
+
} from'payload'
288
+
289
+
exportinterfacePluginTypes {
290
+
/**
291
+
* Collections to enable this plugin for
292
+
*/
293
+
collections?:CollectionSlug[]
294
+
}
295
+
296
+
// Stays generic over whichever collection slug is passed in
// `doc` is typed to the user's collection at `slug`
302
+
returndoc
303
+
}
304
+
returnhook
305
+
}
306
+
```
307
+
308
+
<Bannertype="info">
309
+
Within your plugin's own repository, `GeneratedTypes` is not augmented, so
310
+
these helpers fall back to loose types. To develop against real types,
311
+
generate them inside your `dev/` project (see the `dev:generate-types` script
312
+
in the [plugin template](#plugin-template)) — the concrete types materialize
313
+
once the plugin is installed in a project that has run `payload
314
+
generate:types`.
315
+
</Banner>
316
+
317
+
If your plugin needs to contribute its _own_ reusable types into the user's generated file, extend the JSON schema via [`typescript.schema`](../typescript/generating-types#custom-generated-types).
318
+
276
319
## Best practices
277
320
278
321
In addition to the setup covered above, here are other best practices to follow:
Beyond the concrete interfaces in your generated `payload-types.ts` (`Post`, `User`, etc.), Payload exports a set of **generic type helpers** from the `payload` package. Instead of referencing a single collection by name, these resolve dynamically against _all_ of your collections and globals — which makes them especially useful in [Plugins](../plugins/build-your-own#consuming-generated-types), reusable [Hooks](../hooks/overview), and [Access Control](../access-control/overview) functions, where the exact collection may not be known ahead of time.
41
+
42
+
### How they resolve
43
+
44
+
When you run [`payload generate:types`](./generating-types), the generated file augments a global `GeneratedTypes` interface inside the `payload` module:
45
+
46
+
```ts
47
+
// payload-types.ts (generated)
48
+
declaremodule'payload' {
49
+
exportinterfaceGeneratedTypesextendsConfig {}
50
+
}
51
+
```
52
+
53
+
Every helper below is generic over this augmented interface. Because the augmentation lives in _your_ project, the helpers resolve to _your_ actual collections wherever your code is type-checked — including inside a third-party plugin's source once it is installed in your project. If `GeneratedTypes` has not been augmented (for example, in a plugin's own repository before it is consumed), the helpers gracefully fall back to loose types (`string`, index signatures) rather than erroring.
0 commit comments