fastify / fastify/fastify-multipart
fix: ajvFilePlugin type incompatible with Fastify's ajv.plugins option
- Dominant language
- JavaScript
- Stars
- 539
- Forks
- 126
- PR merge metrics
- No merged PRs in 30d
Description
## Bug Description
The `ajvFilePlugin` type definition is incompatible with Fastify's `ajv.plugins` option, causing TypeScript errors when registering the plugin.
## Current Type
```typescript
export function ajvFilePlugin (ajv: any): void
```
## Expected Type
```typescript
export function ajvFilePlugin (ajv: Ajv): Ajv
```
## Steps to Reproduce
```typescript
import fastify from 'fastify'
import { fastifyMultipart, ajvFilePlugin } from '@fastify/multipart'
const app = fastify({
ajv: {
plugins: [ajvFilePlugin] // TypeScript error here
}
})
app.register(fastifyMultipart)
```
## Error Message
```
error TS2769: No overload matches this call.
The last overload gave the following error.
Type '(ajv: any) => void' is not assignable to type 'Plugin | [Plugin, unknown]'.
Type '(ajv: any) => void' is not assignable to type 'Plugin'.
Type 'void' is not assignable to type 'Ajv'.
```
## Root Cause
Fastify's `ajv.plugins` option expects `Plugin` type from `@fastify/ajv-compiler`, which is defined as:
```typescript
export interface Plugin {
(ajv: Ajv, options?: Opts): Ajv; // Must return Ajv
[prop: string]: any;
}
```
The actual implementation in `index.js` correctly returns `Ajv`:
```javascript
function ajvFilePlugin (ajv) {
return ajv.addKeyword({...}) // addKeyword returns Ajv
}
```
But the type definition incorrectly declares the return type as `void`.
## Proposed Fix
1. Add `ajv` to devDependencies for type definitions
2. Update `types/index.d.ts`:
```typescript
import type Ajv from 'ajv'
// ...
export function ajvFilePlugin (ajv: Ajv): Ajv
```
## Environment
- `@fastify/multipart`: 9.4.0
- `fastify`: 5.7.4
- TypeScript: 5.8.3
Contributor guide
Assessment
This issue has not been assessed yet.