fix: type augmentation of RequestContext (#9035)

### What?

Makes this to actually work
```ts
import type { RequestContext as OriginalRequestContext } from 'payload'

declare module 'payload' {
  // Create a new interface that merges your additional fields with the original one
  export interface RequestContext extends OriginalRequestContext {
    myObject?: string
    // ...
  }
}
```
<img width="502" alt="image"
src="https://github.com/user-attachments/assets/38570d3c-e8a8-48aa-a57d-6d11e79394f5">


### Why?
This is described in our docs
https://payloadcms.com/docs/beta/hooks/context#typescript therefore it
should work.

### How?
In order to get the declaration work, we need to reuse the type from the
root file `payload/src/index.js`. Additionally, removes `RequestContext`
type duplication in both `payload/src/types/index.js` and
`payload/src/index.js`.

Fixes https://github.com/payloadcms/payload/issues/8851
This commit is contained in:
Sasha
2024-11-05 23:14:04 +02:00
committed by GitHub
parent 2eeed4a8ae
commit f52b7c45c0
32 changed files with 76 additions and 69 deletions

View File

@@ -54,6 +54,7 @@ describe('_Community Tests', () => {
data: {
text: 'LOCAL API EXAMPLE',
},
context: {},
})
expect(newPost.text).toEqual('LOCAL API EXAMPLE')

9
test/_community/types.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
import type { RequestContext as OriginalRequestContext } from 'payload'
declare module 'payload' {
// Create a new interface that merges your additional fields with the original one
export interface RequestContext extends OriginalRequestContext {
myObject?: string
// ...
}
}